You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/apis.md
+17
Original file line number
Diff line number
Diff line change
@@ -15,3 +15,20 @@ In addition to [Hooks](/reference/react) and [Components](/reference/react/compo
15
15
*[`lazy`](/reference/react/lazy) lets you defer loading a component's code until it's rendered for the first time.
16
16
*[`memo`](/reference/react/memo) lets your component skip re-renders with same props. Used with [`useMemo`](/reference/react/useMemo) and [`useCallback`.](/reference/react/useCallback)
17
17
*[`startTransition`](/reference/react/startTransition) lets you mark a state update as non-urgent. Similar to [`useTransition`.](/reference/react/useTransition)
18
+
19
+
---
20
+
21
+
## Resource APIs {/*resource-apis*/}
22
+
23
+
*Resources* can be accessed by a component without having them as part of their state. For example, a component can read a message from a Promise or read styling information from a context.
24
+
25
+
To read a value from a resource, use this API:
26
+
27
+
*[`use`](/reference/react/use) lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
Copy file name to clipboardExpand all lines: src/content/reference/react/hooks.md
-18
Original file line number
Diff line number
Diff line change
@@ -106,24 +106,6 @@ To prioritize rendering, use one of these Hooks:
106
106
107
107
---
108
108
109
-
## Resource Hooks {/*resource-hooks*/}
110
-
111
-
*Resources* can be accessed by a component without having them as part of their state. For example, a component can read a message from a Promise or read styling information from a context.
112
-
113
-
To read a value from a resource, use this Hook:
114
-
115
-
- [`use`](/reference/react/use) lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
116
-
117
-
```js
118
-
functionMessageComponent({ messagePromise }) {
119
-
constmessage=use(messagePromise);
120
-
consttheme=use(ThemeContext);
121
-
// ...
122
-
}
123
-
```
124
-
125
-
---
126
-
127
109
## Other Hooks {/*other-hooks*/}
128
110
129
111
These Hooks are mostly useful to library authors and aren't commonly used in the application code.
Copy file name to clipboardExpand all lines: src/content/reference/react/use.md
+15-15
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,13 @@ canary: true
5
5
6
6
<Canary>
7
7
8
-
The `use`Hook is currently only available in React's Canary and experimental channels. Learn more about [React's release channels here](/community/versioning-policy#all-release-channels).
8
+
The `use`API is currently only available in React's Canary and experimental channels. Learn more about [React's release channels here](/community/versioning-policy#all-release-channels).
9
9
10
10
</Canary>
11
11
12
12
<Intro>
13
13
14
-
`use` is a React Hook that lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
14
+
`use` is a React API that lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
15
15
16
16
```js
17
17
constvalue=use(resource);
@@ -38,9 +38,9 @@ function MessageComponent({ messagePromise }) {
38
38
// ...
39
39
```
40
40
41
-
Unlike all other React Hooks, `use` can be called within loops and conditional statements like `if`. Like other React Hooks, the function that calls `use` must be a Component or Hook.
41
+
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.
42
42
43
-
When called with a Promise, the `use`Hook integrates with [`Suspense`](/reference/react/Suspense) and [error boundaries](/reference/react/Component#catching-rendering-errors-with-an-error-boundary). The component calling `use` *suspends* while the Promise passed to `use` is pending. If the component that calls `use` is wrapped in a Suspense boundary, the fallback will be displayed. Once the Promise is resolved, the Suspense fallback is replaced by the rendered components using the data returned by the `use`Hook. If the Promise passed to `use` is rejected, the fallback of the nearest Error Boundary will be displayed.
43
+
When called with a Promise, the `use`API integrates with [`Suspense`](/reference/react/Suspense) and [error boundaries](/reference/react/Component#catching-rendering-errors-with-an-error-boundary). The component calling `use` *suspends* while the Promise passed to `use` is pending. If the component that calls `use` is wrapped in a Suspense boundary, the fallback will be displayed. Once the Promise is resolved, the Suspense fallback is replaced by the rendered components using the data returned by the `use`API. If the Promise passed to `use` is rejected, the fallback of the nearest Error Boundary will be displayed.
44
44
45
45
[See more examples below.](#usage)
46
46
@@ -50,11 +50,11 @@ When called with a Promise, the `use` Hook integrates with [`Suspense`](/referen
50
50
51
51
#### Returns {/*returns*/}
52
52
53
-
The `use`Hook returns the value that was read from the resource like the resolved value of a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
53
+
The `use`API returns the value that was read from the resource like the resolved value of a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
54
54
55
55
#### Caveats {/*caveats*/}
56
56
57
-
* The `use`Hook must be called inside a Component or a Hook.
57
+
* The `use`API must be called inside a Component or a Hook.
58
58
* When fetching data in a [Server Component](/reference/react/use-server), prefer `async` and `await` over `use`. `async` and `await` pick up rendering from the point where `await` was invoked, whereas `use` re-renders the component after the data is resolved.
59
59
* Prefer creating Promises in [Server Components](/reference/react/use-server) and passing them to [Client Components](/reference/react/use-client) over creating Promises in Client Components. Promises created in Client Components are recreated on every render. Promises passed from a Server Component to a Client Component are stable across re-renders. [See this example](#streaming-data-from-server-to-client).
60
60
@@ -230,7 +230,7 @@ export default function App() {
230
230
}
231
231
```
232
232
233
-
The <CodeStep step={2}>Client Component</CodeStep> then takes <CodeStep step={4}>the Promise it received as a prop</CodeStep> and passes it to the <CodeStep step={5}>`use`</CodeStep> Hook. This allows the <CodeStep step={2}>Client Component</CodeStep> to read the value from <CodeStep step={4}>the Promise</CodeStep> that was initially created by the Server Component.
233
+
The <CodeStep step={2}>Client Component</CodeStep> then takes <CodeStep step={4}>the Promise it received as a prop</CodeStep> and passes it to the <CodeStep step={5}>`use`</CodeStep> API. This allows the <CodeStep step={2}>Client Component</CodeStep> to read the value from <CodeStep step={4}>the Promise</CodeStep> that was initially created by the Server Component.
@@ -243,7 +243,7 @@ export function Message({ messagePromise }) {
243
243
return<p>Here is the message: {messageContent}</p>;
244
244
}
245
245
```
246
-
Because <CodeStep step={2}>`Message`</CodeStep> is wrapped in <CodeStep step={3}>[`Suspense`](/reference/react/Suspense)</CodeStep>, the fallback will be displayed until the Promise is resolved. When the Promise is resolved, the value will be read by the <CodeStep step={5}>`use`</CodeStep> Hook and the <CodeStep step={2}>`Message`</CodeStep> component will replace the Suspense fallback.
246
+
Because <CodeStep step={2}>`Message`</CodeStep> is wrapped in <CodeStep step={3}>[`Suspense`](/reference/react/Suspense)</CodeStep>, the fallback will be displayed until the Promise is resolved. When the Promise is resolved, the value will be read by the <CodeStep step={5}>`use`</CodeStep> API and the <CodeStep step={2}>`Message`</CodeStep> component will replace the Suspense fallback.
247
247
248
248
<Sandpack>
249
249
@@ -293,7 +293,7 @@ export default function App() {
293
293
```js src/index.js hidden
294
294
// TODO: update to import from stable
295
295
// react instead of canary once the `use`
296
-
//Hook is in a stable release of React
296
+
//API is in a stable release of React
297
297
importReact, { StrictMode } from'react';
298
298
import { createRoot } from'react-dom/client';
299
299
import'./styles.css';
@@ -334,7 +334,7 @@ When passing a Promise from a Server Component to a Client Component, its resolv
334
334
335
335
#### Should I resolve a Promise in a Server or Client Component? {/*resolve-promise-in-server-or-client-component*/}
336
336
337
-
A Promise can be passed from a Server Component to a Client Component and resolved in the Client Component with the `use`Hook. You can also resolve the Promise in a Server Component with `await` and pass the required data to the Client Component as a prop.
337
+
A Promise can be passed from a Server Component to a Client Component and resolved in the Client Component with the `use`API. You can also resolve the Promise in a Server Component with `await` and pass the required data to the Client Component as a prop.
338
338
339
339
```js
340
340
exportdefaultasyncfunctionApp() {
@@ -360,7 +360,7 @@ In some cases a Promise passed to `use` could be rejected. You can handle reject
360
360
361
361
#### Displaying an error to users with an error boundary {/*displaying-an-error-to-users-with-error-boundary*/}
362
362
363
-
If you'd like to display an error to your users when a Promise is rejected, you can use an [error boundary](/reference/react/Component#catching-rendering-errors-with-an-error-boundary). To use an error boundary, wrap the component where you are calling the `use`Hook in an error boundary. If the Promise passed to `use` is rejected the fallback for the error boundary will be displayed.
363
+
If you'd like to display an error to your users when a Promise is rejected, you can use an [error boundary](/reference/react/Component#catching-rendering-errors-with-an-error-boundary). To use an error boundary, wrap the component where you are calling the `use`API in an error boundary. If the Promise passed to `use` is rejected the fallback for the error boundary will be displayed.
364
364
365
365
<Sandpack>
366
366
@@ -413,7 +413,7 @@ export default function App() {
413
413
```js src/index.js hidden
414
414
// TODO: update to import from stable
415
415
// react instead of canary once the `use`
416
-
//Hook is in a stable release of React
416
+
//API is in a stable release of React
417
417
importReact, { StrictMode } from'react';
418
418
import { createRoot } from'react-dom/client';
419
419
import'./styles.css';
@@ -474,9 +474,9 @@ To use the Promise's <CodeStep step={1}>`catch`</CodeStep> method, call <CodeSte
474
474
475
475
### "Suspense Exception: This is not a real error!" {/*suspense-exception-error*/}
476
476
477
-
You are either calling `use` outside of a React component or Hook function, or calling `use` in a try–catch block. If you are calling `use` inside a try–catch block, wrap your component in an error boundary, or call the Promise's `catch` to catch the error and resolve the Promise with another value. [See these examples](#dealing-with-rejected-promises).
477
+
You are either calling `use` outside of a React Component or Hook function, or calling `use` in a try–catch block. If you are calling `use` inside a try–catch block, wrap your component in an error boundary, or call the Promise's `catch` to catch the error and resolve the Promise with another value. [See these examples](#dealing-with-rejected-promises).
478
478
479
-
If you are calling `use` outside a React component or Hook function, move the `use` call to a React component or Hook function.
479
+
If you are calling `use` outside a React Component or Hook function, move the `use` call to a React Component or Hook function.
480
480
481
481
```jsx
482
482
functionMessageComponent({messagePromise}) {
@@ -486,7 +486,7 @@ function MessageComponent({messagePromise}) {
486
486
// ...
487
487
```
488
488
489
-
Instead, call `use` outside any component closures, where the function that calls `use` is a component or Hook.
489
+
Instead, call `use` outside any component closures, where the function that calls `use` is a Component or Hook.
0 commit comments