Skip to content

Commit 53011ed

Browse files
authored
Update docs 1 (#71812)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent 5ab28cd commit 53011ed

35 files changed

+867
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Directives
3+
description: Directives are used to modify the behavior of your Next.js application.
4+
---
5+
6+
The following directives are available:

docs/02-app/02-api-reference/01-directives/use-cache.mdx

Lines changed: 381 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: use client
3+
description: Learn how to use the use client directive to render a component on the client.
4+
related:
5+
description: React documentation for use client.
6+
links:
7+
- https://react.dev/reference/rsc/use-client
8+
---
9+
10+
The `use client` directive designates a component to be rendered on the **client side** and should be used when creating interactive user interfaces (UI) that require client-side JavaScript capabilities, such as state management, event handling, and access to browser APIs. This is a React feature.
11+
12+
## Usage
13+
14+
To designate a component as a Client Component, add the `use client` directive **at the top of the file**, before any imports:
15+
16+
````tsx filename="app/components/counter.tsx" highlight={1} switcher
17+
'use client'
18+
19+
import { useState } from 'react'
20+
21+
export default function Counter() {
22+
const [count, setCount] = useState(0)
23+
24+
return (
25+
<div>
26+
<p>Count: {count}</p>
27+
<button onClick={() => setCount(count + 1)}>Increment</button>
28+
</div>
29+
)
30+
}
31+
````
32+
33+
````jsx filename="app/components/counter.js" highlight={1} switcher
34+
'use client'
35+
36+
import { useState } from 'react'
37+
38+
export default function Counter() {
39+
const [count, setCount] = useState(0)
40+
41+
return (
42+
<div>
43+
<p>Count: {count}</p>
44+
<button onClick={() => setCount(count + 1)}>Increment</button>
45+
</div>
46+
)
47+
}
48+
````
49+
50+
## Nesting Client Components within Server Components
51+
52+
Combining Server and Client Components allows you to build applications that are both performant and interactive:
53+
54+
1. **Server Components**: Use for static content, data fetching, and SEO-friendly elements.
55+
2. **Client Components**: Use for interactive elements that require state, effects, or browser APIs.
56+
3. **Component composition**: Nest Client Components within Server Components as needed for a clear separation of server and client logic.
57+
58+
In the following example:
59+
60+
- `Header` is a Server Component handling static content.
61+
- `Counter` is a Client Component enabling interactivity within the page.
62+
63+
```tsx filename="app/page.tsx" highlight={2,8} switcher
64+
import Header from './header'
65+
import Counter from './counter' // This is a Client Component
66+
67+
export default function Page() {
68+
return (
69+
<div>
70+
<Header />
71+
<Counter />
72+
</div>
73+
)
74+
}
75+
```
76+
77+
```jsx filename="app/page.js" highlight={2,8} switcher
78+
import Header from './header'
79+
import Counter from './counter' // This is a Client Component
80+
81+
export default function Page() {
82+
return (
83+
<div>
84+
<Header />
85+
<Counter />
86+
</div>
87+
)
88+
}
89+
```
90+
91+
92+
## Reference
93+
94+
See the [React documentation](https://react.dev/reference/rsc/use-client) for more information on `use client`.
95+
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
title: use server
3+
description: Learn how to use the use server directive to execute code on the server.
4+
related:
5+
description: React documentation for use server.
6+
links:
7+
- https://react.dev/reference/rsc/use-server
8+
---
9+
10+
The `use server` directive designates a function or file to be executed on the **server side**. It can be used at the top of a file to indicate that all functions in the file are server-side, or inline at the top of a function to mark the function as a [Server Function](https://19.react.dev/reference/rsc/server-functions). This is a React feature.
11+
12+
## Using `use server` at the top of a file
13+
14+
The following example shows a file with a `use server` directive at the top. All functions in the file are executed on the server.
15+
16+
```tsx filename="app/actions.ts" highlight={1} switcher
17+
'use server'
18+
import { db } from '@/lib/db' // Your database client
19+
20+
export async function createUser(data: { name: string; email: string }) {
21+
const user = await db.user.create({ data })
22+
return user
23+
}
24+
```
25+
26+
```jsx filename="app/actions.js" highlight={1} switcher
27+
'use server'
28+
import { db } from '@/lib/db' // Your database client
29+
30+
export async function createUser(data) {
31+
const user = await db.user.create({ data })
32+
return user
33+
}
34+
```
35+
36+
### Using Server Functions in a Client Component
37+
38+
To use Server Functions in Client Components you need to create your Server Functions in a dedicated file using the `use server` directive at the top of the file. These Server Functions can then be imported into Client and Server Components and executed.
39+
40+
Assuming you have a `fetchUsers` Server Function in `actions.ts`:
41+
42+
```tsx filename="app/actions.ts" highlight={1} switcher
43+
'use server'
44+
import { db } from '@/lib/db' // Your database client
45+
46+
export async function fetchUsers() {
47+
const users = await db.user.findMany()
48+
return users
49+
}
50+
```
51+
52+
```jsx filename="app/actions.js" highlight={1} switcher
53+
'use server'
54+
import { db } from '@/lib/db' // Your database client
55+
56+
export async function fetchUsers() {
57+
const users = await db.user.findMany()
58+
return users
59+
}
60+
```
61+
62+
Then you can import the `fetchUsers` Server Function into a Client Component and execute it on the client-side.
63+
64+
```tsx filename="app/components/my-button.tsx" highlight={1,2,8} switcher
65+
'use client'
66+
import { fetchUsers } from '../actions'
67+
68+
export default function MyButton() {
69+
return <button onClick={() => fetchUsers()}>Fetch Users</button>
70+
}
71+
```
72+
73+
```jsx filename="app/components/my-button.js" highlight={1,2,8} switcher
74+
'use client'
75+
import { fetchUsers } from '../actions'
76+
77+
export default function MyButton() {
78+
return <button onClick={() => fetchUsers()}>Fetch Users</button>
79+
}
80+
```
81+
82+
83+
## Using `use server` inline
84+
85+
In the following example, `use server` is used inline at the top of a function to mark it as a [Server Function](https://19.react.dev/reference/rsc/server-functions):
86+
87+
```tsx filename="app/page.tsx" highlight={5} switcher
88+
import { db } from '@/lib/db' // Your database client
89+
90+
export default function UserList() {
91+
async function fetchUsers() {
92+
'use server'
93+
const users = await db.user.findMany()
94+
return users
95+
}
96+
97+
return <button onClick={() => fetchUsers()}>Fetch Users</button>
98+
}
99+
```
100+
101+
```jsx filename="app/page.js" highlight={5} switcher
102+
import { db } from '@/lib/db' // Your database client
103+
104+
export default function UserList() {
105+
async function fetchUsers() {
106+
'use server'
107+
const users = await db.user.findMany()
108+
return users
109+
}
110+
111+
return <button onClick={() => fetchUsers()}>Fetch Users</button>
112+
}
113+
```
114+
115+
## Security considerations
116+
117+
When using the `use server` directive, it's important to ensure that all server-side logic is secure and that sensitive data remains protected.
118+
119+
### Authentication and authorization
120+
121+
Always authenticate and authorize users before performing sensitive server-side operations.
122+
123+
```tsx filename="app/actions.ts" highlight={1,7,8,9,10} switcher
124+
'use server'
125+
126+
import { db } from '@/lib/db' // Your database client
127+
import { authenticate } from '@/lib/auth' // Your authentication library
128+
129+
export async function createUser(data: { name: string; email: string }, token: string) {
130+
const user = authenticate(token)
131+
if (!user) {
132+
throw new Error('Unauthorized')
133+
}
134+
const newUser = await db.user.create({ data })
135+
return newUser
136+
}
137+
```
138+
139+
```jsx filename="app/actions.js" highlight={1,7,8,9,10} switcher
140+
'use server'
141+
142+
import { db } from '@/lib/db' // Your database client
143+
import { authenticate } from '@/lib/auth' // Your authentication library
144+
145+
export async function createUser(data, token) {
146+
const user = authenticate(token)
147+
if (!user) {
148+
throw new Error('Unauthorized')
149+
}
150+
const newUser = await db.user.create({ data })
151+
return newUser
152+
}
153+
```
154+
155+
156+
## Reference
157+
158+
See the [React documentation](https://react.dev/reference/rsc/use-server) for more information on `use server`.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)