Skip to content

Commit

Permalink
Merge pull request #474 from reactjs/sync-292534e9
Browse files Browse the repository at this point in the history
Sync with react.dev @ 292534e
  • Loading branch information
alioguzhan committed Jul 4, 2023
2 parents ad959cc + 63994a3 commit d73a28d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/content/reference/react/directives.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: "Directives"
---

<Intro>

React uses two directives to provide instructions to [bundlers compatible with React Server Components](/learn/start-a-new-react-project#bleeding-edge-react-frameworks).

</Intro>

---

## Source code directives {/*source-code-directives*/}

* [`'use client'`](/reference/react/use-client) marks source files whose components execute on the client.
* [`'use server'`](/reference/react/use-server) marks server-side functions that can be called from client-side code.
57 changes: 57 additions & 0 deletions src/content/reference/react/use-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: "'use client'"
---

<Note>

These directives are needed only if you're [using React Server Components](/learn/start-a-new-react-project#bleeding-edge-react-frameworks) or building a library compatible with them.

</Note>


<Intro>

`'use client'` marks source files whose components execute on the client.

</Intro>

<InlineToc />

---

## Reference {/*reference*/}

### `'use client'` {/*use-client*/}

Add `'use client';` at the very top of a file to mark that the file (including any child components it uses) executes on the client, regardless of where it's imported.

```js
'use client';

import { useState } from 'react';

export default function RichTextEditor(props) {
// ...
```
When a file marked `'use client'` is imported from a server component, [compatible bundlers](/learn/start-a-new-react-project#bleeding-edge-react-frameworks) will treat the import as the "cut-off point" between server-only code and client code. Components at or below this point in the module graph can use client-only React features like [`useState`](/reference/react/useState).
#### Caveats {/*caveats*/}
* It's not necessary to add `'use client'` to every file that uses client-only React features, only the files that are imported from server component files. `'use client'` denotes the _boundary_ between server-only and client code; any components further down the tree will automatically be executed on the client. In order to be rendered from server components, components exported from `'use client'` files must have serializable props.
* When a `'use client'` file is imported from a server file, the imported values can be rendered as a React component or passed via props to a client component. Any other use will throw an exception.
* When a `'use client'` file is imported from another client file, the directive has no effect. This allows you to write client-only components that are simultaneously usable from server and client components.
* All the code in `'use client'` file as well as any modules it imports (directly or indirectly) will become a part of the client module graph and must be sent to and executed by the client in order to be rendered by the browser. To reduce client bundle size and take full advantage of the server, move state (and the `'use client'` directives) lower in the tree when possible, and pass rendered server components [as children](/learn/passing-props-to-a-component#passing-jsx-as-children) to client components.
* Because props are serialized across the server–client boundary, note that the placement of these directives can affect the amount of data sent to the client; avoid data structures that are larger than necessary.
* Components like a `<MarkdownRenderer>` that use neither server-only nor client-only features should generally not be marked with `'use client'`. That way, they can render exclusively on the server when used from a server component, but they'll be added to the client bundle when used from a client component.
* Libraries published to npm should include `'use client'` on exported React components that can be rendered with serializable props that use client-only React features, to allow those components to be imported and rendered by server components. Otherwise, users will need to wrap library components in their own `'use client'` files which can be cumbersome and prevents the library from moving logic to the server later. When publishing prebundled files to npm, ensure that `'use client'` source files end up in a bundle marked with `'use client'`, separate from any bundle containing exports that can be used directly on the server.
* Client components will still run as part of server-side rendering (SSR) or build-time static site generation (SSG), which act as clients to transform React components' initial render output to HTML that can be rendered before JavaScript bundles are downloaded. But they can't use server-only features like reading directly from a database.
* Directives like `'use client'` must be at the very beginning of a file, above any imports or other code (comments above directives are OK). They must be written with single or double quotes, not backticks. (The `'use xyz'` directive format somewhat resembles the `useXyz()` Hook naming convention, but the similarity is coincidental.)
## Usage {/*usage*/}
<Wip>
This section is incomplete. See also the [Next.js documentation for Server Components](https://beta.nextjs.org/docs/rendering/server-and-client-components).
</Wip>
48 changes: 48 additions & 0 deletions src/content/reference/react/use-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: "'use server'"
---

<Wip>

This section is incomplete.

These directives are needed only if you're [using React Server Components](/learn/start-a-new-react-project#bleeding-edge-react-frameworks) or building a library compatible with them.

</Wip>


<Intro>

`'use server'` marks server-side functions that can be called from client-side code.

</Intro>

<InlineToc />

---

## Reference {/*reference*/}

### `'use server'` {/*use-server*/}

Add `'use server';` at the very top of an async function to mark that the function can be executed by the client.

```js
async function addToCart(data) {
'use server';
// ...
}

// <ProductDetailPage addToCart={addToCart} />
```

This function can be passed to the client. When called on the client, it will make a network request to the server that includes a serialized copy of any arguments passed. If the server function returns a value, that value will be serialized and returned to the client.

Alternatively, add `'use server';` at the very top of a file to mark all exports within that file as async server functions that can be used anywhere, including imported in client component files.

#### Caveats {/*caveats*/}

* Remember that parameters to functions marked with `'use server'` are fully client-controlled. For security, always treat them as untrusted input, making sure to validate and escape the arguments as appropriate.
* To avoid the confusion that might result from mixing client- and server-side code in the same file, `'use server'` can only be used in server-side files; the resulting functions can be passed to client components through props.
* Because the underlying network calls are always asynchronous, `'use server'` can be used only on async functions.
* Directives like `'use server'` must be at the very beginning of their function or file, above any other code including imports (comments above directives are OK). They must be written with single or double quotes, not backticks. (The `'use xyz'` directive format somewhat resembles the `useXyz()` Hook naming convention, but the similarity is coincidental.)
4 changes: 2 additions & 2 deletions src/content/reference/react/useRef.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function MyComponent() {
Eğer render sırasında bir şey okumak [veya yazmak](/reference/react/useState#storing-information-from-previous-renders) *zorunda* kalırsanız, bunun yerine [state kullanın](/reference/react/useState).
Bu kuralları ihlal ettiğinizde, bileşeniniz hala çalışabilir, ancak React'e eklediğimiz yeni özelliklerin çoğu bu beklentilere bağlı olacaktır. [Bileşenlerinizi saf tutma](/learn/keeping-components-pure#where-you-can-cause-side-effects) hakkında daha fazla bilgi edinin.
Bu kuralları ihlal ettiğinizde, bileşeniniz hala çalışabilir, ancak React'e eklediğimiz yeni özelliklerin çoğu bu beklentilere bağlı olacaktır. [Bileşenlerinizi saf tutma](/learn/keeping-components-pure#where-you-_can_-cause-side-effects) hakkında daha fazla bilgi edinin.
</Pitfall>
Expand Down Expand Up @@ -595,4 +595,4 @@ export default MyInput;
Böylece ana bileşen ona bir ref alabilir.
[Başka bir bileşenin DOM elemanına erişme](/learn/manipulating-the-dom-with-refs#accessing-another-components-dom-nodes) hakkında daha fazla bilgi edinin.
[Başka bir bileşenin DOM elemanına erişme](/learn/manipulating-the-dom-with-refs#accessing-another-components-dom-nodes) hakkında daha fazla bilgi edinin.
14 changes: 14 additions & 0 deletions src/sidebarReference.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@
}
]
},
{
"title": "Directives",
"path": "/reference/react/directives",
"routes": [
{
"title": "'use client'",
"path": "/reference/react/use-client"
},
{
"title": "'use server'",
"path": "/reference/react/use-server"
}
]
},
{
"hasSectionHeader": true,
"sectionHeader": "react-dom@18.2.0"
Expand Down

0 comments on commit d73a28d

Please sign in to comment.