-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'canary' into 10-24-ensure_dio_development_segment_error…
…s_are_cleared_after_correcting
- Loading branch information
Showing
52 changed files
with
904 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: Directives | ||
description: Directives are used to modify the behavior of your Next.js application. | ||
--- | ||
|
||
The following directives are available: |
382 changes: 382 additions & 0 deletions
382
docs/02-app/02-api-reference/01-directives/use-cache.mdx
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
title: use client | ||
description: Learn how to use the use client directive to render a component on the client. | ||
related: | ||
description: React documentation for use client. | ||
links: | ||
- https://react.dev/reference/rsc/use-client | ||
--- | ||
|
||
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. | ||
|
||
## Usage | ||
|
||
To designate a component as a Client Component, add the `use client` directive **at the top of the file**, before any imports: | ||
|
||
```tsx filename="app/components/counter.tsx" highlight={1} switcher | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
|
||
export default function Counter() { | ||
const [count, setCount] = useState(0) | ||
|
||
return ( | ||
<div> | ||
<p>Count: {count}</p> | ||
<button onClick={() => setCount(count + 1)}>Increment</button> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
```jsx filename="app/components/counter.js" highlight={1} switcher | ||
'use client' | ||
|
||
import { useState } from 'react' | ||
|
||
export default function Counter() { | ||
const [count, setCount] = useState(0) | ||
|
||
return ( | ||
<div> | ||
<p>Count: {count}</p> | ||
<button onClick={() => setCount(count + 1)}>Increment</button> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
## Nesting Client Components within Server Components | ||
|
||
Combining Server and Client Components allows you to build applications that are both performant and interactive: | ||
|
||
1. **Server Components**: Use for static content, data fetching, and SEO-friendly elements. | ||
2. **Client Components**: Use for interactive elements that require state, effects, or browser APIs. | ||
3. **Component composition**: Nest Client Components within Server Components as needed for a clear separation of server and client logic. | ||
|
||
In the following example: | ||
|
||
- `Header` is a Server Component handling static content. | ||
- `Counter` is a Client Component enabling interactivity within the page. | ||
|
||
```tsx filename="app/page.tsx" highlight={2,8} switcher | ||
import Header from './header' | ||
import Counter from './counter' // This is a Client Component | ||
export default function Page() { | ||
return ( | ||
<div> | ||
<Header /> | ||
<Counter /> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
```jsx filename="app/page.js" highlight={2,8} switcher | ||
import Header from './header' | ||
import Counter from './counter' // This is a Client Component | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<Header /> | ||
<Counter /> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
## Reference | ||
|
||
See the [React documentation](https://react.dev/reference/rsc/use-client) for more information on `use client`. |
159 changes: 159 additions & 0 deletions
159
docs/02-app/02-api-reference/01-directives/use-server.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
--- | ||
title: use server | ||
description: Learn how to use the use server directive to execute code on the server. | ||
related: | ||
description: React documentation for use server. | ||
links: | ||
- https://react.dev/reference/rsc/use-server | ||
--- | ||
|
||
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. | ||
|
||
## Using `use server` at the top of a file | ||
|
||
The following example shows a file with a `use server` directive at the top. All functions in the file are executed on the server. | ||
|
||
```tsx filename="app/actions.ts" highlight={1} switcher | ||
'use server' | ||
import { db } from '@/lib/db' // Your database client | ||
export async function createUser(data: { name: string; email: string }) { | ||
const user = await db.user.create({ data }) | ||
return user | ||
} | ||
``` | ||
|
||
```jsx filename="app/actions.js" highlight={1} switcher | ||
'use server' | ||
import { db } from '@/lib/db' // Your database client | ||
|
||
export async function createUser(data) { | ||
const user = await db.user.create({ data }) | ||
return user | ||
} | ||
``` | ||
|
||
### Using Server Functions in a Client Component | ||
|
||
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. | ||
|
||
Assuming you have a `fetchUsers` Server Function in `actions.ts`: | ||
|
||
```tsx filename="app/actions.ts" highlight={1} switcher | ||
'use server' | ||
import { db } from '@/lib/db' // Your database client | ||
export async function fetchUsers() { | ||
const users = await db.user.findMany() | ||
return users | ||
} | ||
``` | ||
|
||
```jsx filename="app/actions.js" highlight={1} switcher | ||
'use server' | ||
import { db } from '@/lib/db' // Your database client | ||
|
||
export async function fetchUsers() { | ||
const users = await db.user.findMany() | ||
return users | ||
} | ||
``` | ||
|
||
Then you can import the `fetchUsers` Server Function into a Client Component and execute it on the client-side. | ||
|
||
```tsx filename="app/components/my-button.tsx" highlight={1,2,8} switcher | ||
'use client' | ||
import { fetchUsers } from '../actions' | ||
|
||
export default function MyButton() { | ||
return <button onClick={() => fetchUsers()}>Fetch Users</button> | ||
} | ||
``` | ||
|
||
```jsx filename="app/components/my-button.js" highlight={1,2,8} switcher | ||
'use client' | ||
import { fetchUsers } from '../actions' | ||
|
||
export default function MyButton() { | ||
return <button onClick={() => fetchUsers()}>Fetch Users</button> | ||
} | ||
``` | ||
|
||
## Using `use server` inline | ||
|
||
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): | ||
|
||
```tsx filename="app/page.tsx" highlight={5} switcher | ||
import { db } from '@/lib/db' // Your database client | ||
export default function UserList() { | ||
async function fetchUsers() { | ||
'use server' | ||
const users = await db.user.findMany() | ||
return users | ||
} | ||
|
||
return <button onClick={() => fetchUsers()}>Fetch Users</button> | ||
} | ||
``` | ||
|
||
```jsx filename="app/page.js" highlight={5} switcher | ||
import { db } from '@/lib/db' // Your database client | ||
|
||
export default function UserList() { | ||
async function fetchUsers() { | ||
'use server' | ||
const users = await db.user.findMany() | ||
return users | ||
} | ||
|
||
return <button onClick={() => fetchUsers()}>Fetch Users</button> | ||
} | ||
``` | ||
|
||
## Security considerations | ||
|
||
When using the `use server` directive, it's important to ensure that all server-side logic is secure and that sensitive data remains protected. | ||
|
||
### Authentication and authorization | ||
|
||
Always authenticate and authorize users before performing sensitive server-side operations. | ||
|
||
```tsx filename="app/actions.ts" highlight={1,7,8,9,10} switcher | ||
'use server' | ||
|
||
import { db } from '@/lib/db' // Your database client | ||
import { authenticate } from '@/lib/auth' // Your authentication library | ||
export async function createUser( | ||
data: { name: string; email: string }, | ||
token: string | ||
) { | ||
const user = authenticate(token) | ||
if (!user) { | ||
throw new Error('Unauthorized') | ||
} | ||
const newUser = await db.user.create({ data }) | ||
return newUser | ||
} | ||
``` | ||
|
||
```jsx filename="app/actions.js" highlight={1,7,8,9,10} switcher | ||
'use server' | ||
|
||
import { db } from '@/lib/db' // Your database client | ||
import { authenticate } from '@/lib/auth' // Your authentication library | ||
|
||
export async function createUser(data, token) { | ||
const user = authenticate(token) | ||
if (!user) { | ||
throw new Error('Unauthorized') | ||
} | ||
const newUser = await db.user.create({ data }) | ||
return newUser | ||
} | ||
``` | ||
|
||
## Reference | ||
|
||
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.