Skip to content

Commit 12aa604

Browse files
committed
Use recipes instead
1 parent 40ace05 commit 12aa604

File tree

1 file changed

+79
-15
lines changed
  • src/content/reference/react

1 file changed

+79
-15
lines changed

src/content/reference/react/use.md

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,27 @@ To use the Promise's <CodeStep step={1}>`catch`</CodeStep> method, call <CodeSte
440440
441441
React will read the `status` field on Promise subclasses to synchronously read the value without waiting for a microtask. If the Promise is already settled (resolved or rejected), React can read the value immediately without suspending and showing a fallback if the update was not part of a Transition (e.g. [`ReactDOM.flushSync()`](/reference/react-dom/flushSync)).
442442
443-
React will set the `status` field itself if the passed Promise does not have this field set. Suspense-enabled libraries should set the `status` field on the Promises they create to avoid unnecessary fallbacks. Calling `use` conditionally depending on whether a Promise is settled or not is discouraged. `use` should be called unconditionally so that React DevTools can show that the Component may suspend on data.
443+
React will set the `status` field itself if the passed Promise does not have this field set. Suspense-enabled libraries should set the `status` field on the Promises they create to avoid unnecessary fallbacks.
444+
445+
Calling `use` conditionally depending on whether a Promise is settled or not is discouraged. `use` should be called unconditionally so that React DevTools can show that the Component may suspend on data.
446+
447+
<Recipes titleText="Difference between setting the status field in your library and not setting the status field" titleId="difference-between-setting-the-status-field-in-your-library-and-not-setting-the-status-field">
448+
449+
#### Passing a basic Promise {/*passing-a-basic-promise*/}
444450
445451
<Sandpack>
446452
447453
```js src/App.js active
448454
import { Suspense, use, useState } from "react";
449-
import { preload } from "./data-fetching.js";
455+
456+
function preloadUser(id) {
457+
// This is not a real implementation of getting the Promise for the user.
458+
// A real implementation would probably call `fetch` or another data fetching method.
459+
// The actual implementation should cache the Promise.
460+
const promise = Promise.resolve(`User #${id}`);
461+
462+
return promise;
463+
}
450464

451465
function UserDetails({ userUsable }) {
452466
const user = use(userUsable);
@@ -460,25 +474,21 @@ export default function App() {
460474

461475
return (
462476
<div>
463-
<p>
464-
Passing the Promise without the <code>status</code> field will show the
465-
fallback because the Promise resolves in the next microtask.
466-
</p>
467477
<button
468478
onClick={() => {
469-
setUser(Promise.resolve("User #2"));
470-
setUserId(2);
479+
setUser(preloadUser(1));
480+
setUserId(1);
471481
}}
472482
>
473-
Load Promise not integrated with Suspense
483+
Load User #1
474484
</button>
475485
<button
476486
onClick={() => {
477-
setUser(preload(1));
478-
setUserId(1);
487+
setUser(preloadUser(2));
488+
setUserId(2);
479489
}}
480490
>
481-
Load Promise integrated with Suspense
491+
Load User #2
482492
</button>
483493
<Suspense key={userId} fallback={<p>Loading user...</p>}>
484494
{userUsable ? (
@@ -492,11 +502,22 @@ export default function App() {
492502
}
493503
```
494504
505+
</Sandpack>
506+
507+
<Solution />
495508
496-
```js src/data-fetching.js
497-
export function preload(id) {
509+
#### Passing the Promise with a `status` field {/*passing-the-promise-with-the-status-field*/}
510+
511+
512+
<Sandpack>
513+
514+
```js src/App.js active
515+
import { Suspense, use, useState } from "react";
516+
517+
function preloadUser(id) {
498518
// This is not a real implementation of getting the Promise for the user.
499-
// The actual implementation should cache the Promise
519+
// A real implementation would probably call `fetch` or another data fetching method.
520+
// The actual implementation should cache the Promise.
500521
const promise = Promise.resolve(`User #${id}`);
501522

502523
// Setting the `status` field allows React to synchronously read
@@ -514,10 +535,53 @@ export function preload(id) {
514535
);
515536
return promise;
516537
}
538+
539+
function UserDetails({ userUsable }) {
540+
const user = use(userUsable);
541+
return <p>Hello, {user}!</p>;
542+
}
543+
544+
export default function App() {
545+
const [userId, setUserId] = useState(null);
546+
// The initial
547+
const [userUsable, setUser] = useState(null);
548+
549+
return (
550+
<div>
551+
<button
552+
onClick={() => {
553+
setUser(preloadUser(1));
554+
setUserId(1);
555+
}}
556+
>
557+
Load User #1
558+
</button>
559+
<button
560+
onClick={() => {
561+
setUser(preloadUser(2));
562+
setUserId(2);
563+
}}
564+
>
565+
Load User #2
566+
</button>
567+
<Suspense key={userId} fallback={<p>Loading user...</p>}>
568+
{userUsable ? (
569+
<UserDetails userUsable={userUsable} />
570+
) : (
571+
<p>No user selected</p>
572+
)}
573+
</Suspense>
574+
</div>
575+
);
576+
}
517577
```
518578
519579
</Sandpack>
520580
581+
<Solution />
582+
583+
</Recipes>
584+
521585
## Troubleshooting {/*troubleshooting*/}
522586
523587
### "Suspense Exception: This is not a real error!" {/*suspense-exception-error*/}

0 commit comments

Comments
 (0)