Skip to content

Commit 66a74fd

Browse files
authored
Merge branch 'canary' into 10-24-ensure_dio_development_segment_errors_are_cleared_after_correcting
2 parents 7fd8989 + a549889 commit 66a74fd

File tree

52 files changed

+904
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+904
-32
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: 382 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
## Reference
92+
93+
See the [React documentation](https://react.dev/reference/rsc/use-client) for more information on `use client`.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
## Using `use server` inline
83+
84+
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):
85+
86+
```tsx filename="app/page.tsx" highlight={5} switcher
87+
import { db } from '@/lib/db' // Your database client
88+
89+
export default function UserList() {
90+
async function fetchUsers() {
91+
'use server'
92+
const users = await db.user.findMany()
93+
return users
94+
}
95+
96+
return <button onClick={() => fetchUsers()}>Fetch Users</button>
97+
}
98+
```
99+
100+
```jsx filename="app/page.js" highlight={5} switcher
101+
import { db } from '@/lib/db' // Your database client
102+
103+
export default function UserList() {
104+
async function fetchUsers() {
105+
'use server'
106+
const users = await db.user.findMany()
107+
return users
108+
}
109+
110+
return <button onClick={() => fetchUsers()}>Fetch Users</button>
111+
}
112+
```
113+
114+
## Security considerations
115+
116+
When using the `use server` directive, it's important to ensure that all server-side logic is secure and that sensitive data remains protected.
117+
118+
### Authentication and authorization
119+
120+
Always authenticate and authorize users before performing sensitive server-side operations.
121+
122+
```tsx filename="app/actions.ts" highlight={1,7,8,9,10} switcher
123+
'use server'
124+
125+
import { db } from '@/lib/db' // Your database client
126+
import { authenticate } from '@/lib/auth' // Your authentication library
127+
128+
export async function createUser(
129+
data: { name: string; email: string },
130+
token: string
131+
) {
132+
const user = authenticate(token)
133+
if (!user) {
134+
throw new Error('Unauthorized')
135+
}
136+
const newUser = await db.user.create({ data })
137+
return newUser
138+
}
139+
```
140+
141+
```jsx filename="app/actions.js" highlight={1,7,8,9,10} switcher
142+
'use server'
143+
144+
import { db } from '@/lib/db' // Your database client
145+
import { authenticate } from '@/lib/auth' // Your authentication library
146+
147+
export async function createUser(data, token) {
148+
const user = authenticate(token)
149+
if (!user) {
150+
throw new Error('Unauthorized')
151+
}
152+
const newUser = await db.user.create({ data })
153+
return newUser
154+
}
155+
```
156+
157+
## Reference
158+
159+
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)