Skip to content

Commit 9fb2f0d

Browse files
authored
Move use to APIs (#6774)
1 parent cdd2fdd commit 9fb2f0d

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

src/content/reference/react/apis.md

+17
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,20 @@ In addition to [Hooks](/reference/react) and [Components](/reference/react/compo
1515
* [`lazy`](/reference/react/lazy) lets you defer loading a component's code until it's rendered for the first time.
1616
* [`memo`](/reference/react/memo) lets your component skip re-renders with same props. Used with [`useMemo`](/reference/react/useMemo) and [`useCallback`.](/reference/react/useCallback)
1717
* [`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).
28+
```js
29+
function MessageComponent({ messagePromise }) {
30+
const message = use(messagePromise);
31+
const theme = use(ThemeContext);
32+
// ...
33+
}
34+
```

src/content/reference/react/hooks.md

-18
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,6 @@ To prioritize rendering, use one of these Hooks:
106106
107107
---
108108
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-
function MessageComponent({ messagePromise }) {
119-
const message = use(messagePromise);
120-
const theme = use(ThemeContext);
121-
// ...
122-
}
123-
```
124-
125-
---
126-
127109
## Other Hooks {/*other-hooks*/}
128110
129111
These Hooks are mostly useful to library authors and aren't commonly used in the application code.

src/content/reference/react/use.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ canary: true
55

66
<Canary>
77

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).
99

1010
</Canary>
1111

1212
<Intro>
1313

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).
1515

1616
```js
1717
const value = use(resource);
@@ -38,9 +38,9 @@ function MessageComponent({ messagePromise }) {
3838
// ...
3939
```
4040
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.
4242
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.
4444
4545
[See more examples below.](#usage)
4646
@@ -50,11 +50,11 @@ When called with a Promise, the `use` Hook integrates with [`Suspense`](/referen
5050
5151
#### Returns {/*returns*/}
5252
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).
5454
5555
#### Caveats {/*caveats*/}
5656
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.
5858
* 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.
5959
* 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).
6060
@@ -230,7 +230,7 @@ export default function App() {
230230
}
231231
```
232232
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.
234234
235235
```js [[2, 6, "Message"], [4, 6, "messagePromise"], [4, 7, "messagePromise"], [5, 7, "use"]]
236236
// message.js
@@ -243,7 +243,7 @@ export function Message({ messagePromise }) {
243243
return <p>Here is the message: {messageContent}</p>;
244244
}
245245
```
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.
247247
248248
<Sandpack>
249249
@@ -293,7 +293,7 @@ export default function App() {
293293
```js src/index.js hidden
294294
// TODO: update to import from stable
295295
// 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
297297
import React, { StrictMode } from 'react';
298298
import { createRoot } from 'react-dom/client';
299299
import './styles.css';
@@ -334,7 +334,7 @@ When passing a Promise from a Server Component to a Client Component, its resolv
334334
335335
#### Should I resolve a Promise in a Server or Client Component? {/*resolve-promise-in-server-or-client-component*/}
336336
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.
338338
339339
```js
340340
export default async function App() {
@@ -360,7 +360,7 @@ In some cases a Promise passed to `use` could be rejected. You can handle reject
360360
361361
#### Displaying an error to users with an error boundary {/*displaying-an-error-to-users-with-error-boundary*/}
362362
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.
364364
365365
<Sandpack>
366366
@@ -413,7 +413,7 @@ export default function App() {
413413
```js src/index.js hidden
414414
// TODO: update to import from stable
415415
// 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
417417
import React, { StrictMode } from 'react';
418418
import { createRoot } from 'react-dom/client';
419419
import './styles.css';
@@ -474,9 +474,9 @@ To use the Promise's <CodeStep step={1}>`catch`</CodeStep> method, call <CodeSte
474474
475475
### "Suspense Exception: This is not a real error!" {/*suspense-exception-error*/}
476476
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).
478478
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.
480480
481481
```jsx
482482
function MessageComponent({messagePromise}) {
@@ -486,7 +486,7 @@ function MessageComponent({messagePromise}) {
486486
// ...
487487
```
488488
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.
490490
491491
```jsx
492492
function MessageComponent({messagePromise}) {

src/sidebarReference.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
"title": "Hooks",
1515
"path": "/reference/react/hooks",
1616
"routes": [
17-
{
18-
"title": "use",
19-
"path": "/reference/react/use",
20-
"canary": true
21-
},
2217
{
2318
"title": "useCallback",
2419
"path": "/reference/react/useCallback"
@@ -137,6 +132,11 @@
137132
"title": "startTransition",
138133
"path": "/reference/react/startTransition"
139134
},
135+
{
136+
"title": "use",
137+
"path": "/reference/react/use",
138+
"canary": true
139+
},
140140
{
141141
"title": "experimental_taintObjectReference",
142142
"path": "/reference/react/experimental_taintObjectReference",

0 commit comments

Comments
 (0)