Unverified Commit be56ce3a authored by Vasiliy Vanchuk's avatar Vasiliy Vanchuk Committed by GitHub

docs: correct use term (#89438)

### What?

`use` is not a hook. It's highlighted in the doc

> Unlike React Hooks, use can be called within loops and conditional
statements like if. Like React Hooks, the function that calls use must
be a Component or Hook.


### Why?

It confuses consumers of Next.js and adds field provide misleading
proofs

### How?

Closes NEXT-
Fixes #

-->

---------
Co-authored-by: 's avatarJoseph <joseph.chamochumbi@vercel.com>
Co-authored-by: 's avatarJoseph <sephxd1234@gmail.com>
parent 0b32ddc6
...@@ -284,7 +284,7 @@ export default function LikeButton({ likes }) { ...@@ -284,7 +284,7 @@ export default function LikeButton({ likes }) {
} }
``` ```
Alternatively, you can stream data from a Server Component to a Client Component with the [`use` Hook](https://react.dev/reference/react/use). See an [example](/docs/app/getting-started/fetching-data#streaming-data-with-the-use-hook). Alternatively, you can stream data from a Server Component to a Client Component with the [`use` API](https://react.dev/reference/react/use). See an [example](/docs/app/getting-started/fetching-data#streaming-data-with-the-use-api).
> **Good to know**: Props passed to Client Components need to be [serializable](https://react.dev/reference/react/use-server#serializable-parameters-and-return-values) by React. > **Good to know**: Props passed to Client Components need to be [serializable](https://react.dev/reference/react/use-server#serializable-parameters-and-return-values) by React.
......
...@@ -99,12 +99,12 @@ export default async function Page() { ...@@ -99,12 +99,12 @@ export default async function Page() {
There are two ways to fetch data in Client Components, using: There are two ways to fetch data in Client Components, using:
1. React's [`use` hook](https://react.dev/reference/react/use) 1. React's [`use` API](https://react.dev/reference/react/use)
2. A community library like [SWR](https://swr.vercel.app/) or [React Query](https://tanstack.com/query/latest) 2. A community library like [SWR](https://swr.vercel.app/) or [React Query](https://tanstack.com/query/latest)
#### Streaming data with the `use` hook #### Streaming data with the `use` API
You can use React's [`use` hook](https://react.dev/reference/react/use) to [stream](#streaming) data from the server to client. Start by fetching data in your Server component, and pass the promise to your Client Component as prop: You can use React's [`use` API](https://react.dev/reference/react/use) to [stream](#streaming) data from the server to client. Start by fetching data in your Server component, and pass the promise to your Client Component as prop:
```tsx filename="app/blog/page.tsx" switcher ```tsx filename="app/blog/page.tsx" switcher
import Posts from '@/app/ui/posts' import Posts from '@/app/ui/posts'
...@@ -138,7 +138,7 @@ export default function Page() { ...@@ -138,7 +138,7 @@ export default function Page() {
} }
``` ```
Then, in your Client Component, use the `use` hook to read the promise: Then, in your Client Component, use the `use` API to read the promise:
```tsx filename="app/ui/posts.tsx" switcher ```tsx filename="app/ui/posts.tsx" switcher
'use client' 'use client'
......
...@@ -33,7 +33,7 @@ Let's explore common patterns used to build SPAs and how Next.js solves them. ...@@ -33,7 +33,7 @@ Let's explore common patterns used to build SPAs and how Next.js solves them.
### Using React’s `use` within a Context Provider ### Using React’s `use` within a Context Provider
We recommend fetching data in a parent component (or layout), returning the Promise, and then unwrapping the value in a Client Component with React’s [`use` hook](https://react.dev/reference/react/use). We recommend fetching data in a parent component (or layout), returning the Promise, and then unwrapping the value in a Client Component with React’s [`use` API](https://react.dev/reference/react/use).
Next.js can start data fetching early on the server. In this example, that’s the root layout — the entry point to your application. The server can immediately begin streaming a response to the client. Next.js can start data fetching early on the server. In this example, that’s the root layout — the entry point to your application. The server can immediately begin streaming a response to the client.
...@@ -79,7 +79,7 @@ export default function RootLayout({ children }) { ...@@ -79,7 +79,7 @@ export default function RootLayout({ children }) {
} }
``` ```
While you can [defer and pass a single Promise](/docs/app/getting-started/fetching-data#streaming-data-with-the-use-hook) as a prop to a Client Component, we generally see this pattern paired with a React context provider. This enables easier access from Client Components with a custom React Hook. While you can [defer and pass a single Promise](/docs/app/getting-started/fetching-data#streaming-data-with-the-use-api) as a prop to a Client Component, we generally see this pattern paired with a React context provider. This enables easier access from Client Components with a custom React Hook.
You can forward a Promise to the React context provider: You can forward a Promise to the React context provider:
......
...@@ -43,7 +43,7 @@ Dynamic Segments are passed as the `params` prop to [`layout`](/docs/app/api-ref ...@@ -43,7 +43,7 @@ Dynamic Segments are passed as the `params` prop to [`layout`](/docs/app/api-ref
### In Client Components ### In Client Components
In a Client Component **page**, dynamic segments from props can be accessed using the [`use`](https://react.dev/reference/react/use) hook. In a Client Component **page**, dynamic segments from props can be accessed using the [`use`](https://react.dev/reference/react/use) API.
```tsx filename="app/blog/[slug]/page.tsx" switcher ```tsx filename="app/blog/[slug]/page.tsx" switcher
'use client' 'use client'
......
...@@ -22,7 +22,7 @@ async function ClientComponent() { ...@@ -22,7 +22,7 @@ async function ClientComponent() {
1. **Convert to a Server Component**: If possible, convert your Client Component to a Server Component. This allows you to use `async`/`await` directly in your component. 1. **Convert to a Server Component**: If possible, convert your Client Component to a Server Component. This allows you to use `async`/`await` directly in your component.
2. **Remove the `async` keyword**: If you need to keep it as a Client Component, remove the `async` keyword and handle data fetching differently. 2. **Remove the `async` keyword**: If you need to keep it as a Client Component, remove the `async` keyword and handle data fetching differently.
3. **Use React Hooks for data fetching**: Utilize hooks like `useEffect` for client-side data fetching, or use third-party libraries. 3. **Use React Hooks for data fetching**: Utilize hooks like `useEffect` for client-side data fetching, or use third-party libraries.
4. **Leverage the `use` hook with a Context Provider**: Pass promises to child components using context, then resolve them with the `use` hook. 4. **Leverage the `use` API with a Context Provider**: Pass promises to child components using context, then resolve them with the `use` API.
### Recommended: Server-side data fetching ### Recommended: Server-side data fetching
...@@ -44,7 +44,7 @@ export default async function Page() { ...@@ -44,7 +44,7 @@ export default async function Page() {
### Using `use` with Context Provider ### Using `use` with Context Provider
Another pattern to explore is using the React `use` hook with a Context Provider. This allows you to pass Promises to child components and resolve them using the `use` hook . Here's an example: Another pattern to explore is using the React `use` API with a Context Provider. This allows you to pass Promises to child components and resolve them using the `use` API . Here's an example:
First, let's create a separate file for the context provider: First, let's create a separate file for the context provider:
...@@ -110,7 +110,7 @@ export function BlogPosts() { ...@@ -110,7 +110,7 @@ export function BlogPosts() {
} }
``` ```
This pattern allows you to start data fetching early and pass the Promise down to child components, which can then use the `use` hook to access the data when it's ready. This pattern allows you to start data fetching early and pass the Promise down to child components, which can then use the `use` API to access the data when it's ready.
### Client-side data fetching ### Client-side data fetching
......
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