Unverified Commit 5dc4b954 authored by Jan Kassens's avatar Jan Kassens Committed by GitHub

docs: clarify use client directive as a file level directive (#99703)

Clarifies that `"use client"` is a file level directive and cannot be
placed inside components. Some of the wording suggested to me that it
could work inside of components similar to `"use server"` which is
allowed inside functions.
parent 6194d642
......@@ -185,7 +185,7 @@ See [Interleaving Server and Client Components](/docs/app/getting-started/server
### Reducing JS bundle size
To reduce the size of your client JavaScript bundles, add `'use client'` to specific interactive components instead of marking large parts of your UI as Client Components.
To reduce the size of your client JavaScript bundles, add `'use client'` at the top of the files that define your interactive components instead of marking large parts of your UI as Client Components.
For example, the `<Layout>` component contains mostly static elements like a logo and navigation links, but includes an interactive search bar. `<Search />` needs to be a Client Component, while the rest of the layout can stay a Server Component.
......@@ -427,7 +427,7 @@ To pass server-fetched data through context and read it in Client Components wit
When using a third-party component that relies on client-only features, you can wrap it in a Client Component to ensure it works as expected.
For example, the `<Carousel />` can be imported from the `acme-carousel` package. This component uses `useState`, but it doesn't yet have the `"use client"` directive.
For example, the `<Carousel />` can be imported from the `acme-carousel` package. This component uses `useState`, but the module that defines it doesn't yet have the `"use client"` directive.
If you use `<Carousel />` within a Client Component, it will work as expected:
......
......@@ -74,7 +74,7 @@ export default function Layout({ children }) {
}
```
> Since the `useReportWebVitals` hook requires the `'use client'` directive, the most performant approach is to create a separate component that the root layout imports. This confines the client boundary exclusively to the `WebVitals` component.
> Since the `useReportWebVitals` hook must be used in a Client Component, create a separate file for the `WebVitals` component with `'use client'` at the top, before any imports. Import this component into the root layout to keep the client boundary limited to this module and its imports.
View the [API Reference](/docs/app/api-reference/functions/use-report-web-vitals) for more information.
......
......@@ -267,7 +267,7 @@ Event handlers can be used with the Script component to execute additional code
<AppOnly>
These handlers will only work when `next/script` is imported and used inside of a [Client Component](/docs/app/getting-started/server-and-client-components) where `"use client"` is defined as the first line of code:
These handlers will only work when `next/script` is imported and used inside of a [Client Component](/docs/app/getting-started/server-and-client-components). To create a client entry point, add `"use client"` at the top of the file, before any imports:
```tsx filename="app/page.tsx" switcher
'use client'
......@@ -313,7 +313,7 @@ Refer to the [`next/script`](/docs/app/api-reference/components/script#onload) A
<PagesOnly>
These handlers will only work when `next/script` is imported and used inside of a [Client Component](/docs/app/getting-started/server-and-client-components) where `"use client"` is defined as the first line of code:
In the Pages Router, you can use these handlers in your components without adding the `"use client"` directive:
```tsx filename="pages/index.tsx" switcher
import Script from 'next/script'
......
......@@ -154,7 +154,7 @@ For the practical list of what belongs in each environment, see [When to use Ser
## Crossing the boundary
You mark a Client Component with the [`'use client'`](/docs/app/api-reference/directives/use-client) directive. The directive draws a boundary in the module graph, and two rules determine what crosses it:
You mark a module as a client entry point by adding the [`'use client'`](/docs/app/api-reference/directives/use-client) directive at the top of the file, before any imports. The directive draws a boundary in the module graph, and two rules determine what crosses it:
- **Code** crosses through imports. Whatever a Client Component imports is pulled into the [client bundle](/docs/app/glossary#client-bundles).
- **Data** crosses through props, and it must be [serializable](/docs/app/getting-started/server-and-client-components#passing-data-from-server-to-client-components), so functions like event handlers cannot cross.
......
......@@ -11,7 +11,7 @@ The `'use client'` directive declares an entry point for the components to be re
## Usage
To declare an entry point for the Client Components, add the `'use client'` directive **at the top of the file**, before any imports:
To declare an entry point for the Client Components, add the `'use client'` directive **at the top of the file**, before any imports. The directive applies to the entire module and cannot be placed inside a component or function:
```tsx filename="app/components/counter.tsx" highlight={1} switcher
'use client'
......
......@@ -60,7 +60,7 @@ export default function Layout({ children }) {
}
```
> Since the `useReportWebVitals` hook requires the `'use client'` directive, the most performant approach is to create a separate component that the root layout imports. This confines the client boundary exclusively to the `WebVitals` component.
> Since the `useReportWebVitals` hook must be used in a Client Component, create a separate file for the `WebVitals` component with `'use client'` at the top, before any imports. Import this component into the root layout to keep the client boundary limited to this module and its imports.
</AppOnly>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment