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/use.md
+79-15Lines changed: 79 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -440,13 +440,27 @@ To use the Promise's <CodeStep step={1}>`catch`</CodeStep> method, call <CodeSte
440
440
441
441
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)).
442
442
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*/}
444
450
445
451
<Sandpack>
446
452
447
453
```js src/App.js active
448
454
import { Suspense, use, useState } from"react";
449
-
import { preload } from"./data-fetching.js";
455
+
456
+
functionpreloadUser(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
+
constpromise=Promise.resolve(`User #${id}`);
461
+
462
+
return promise;
463
+
}
450
464
451
465
functionUserDetails({ userUsable }) {
452
466
constuser=use(userUsable);
@@ -460,25 +474,21 @@ export default function App() {
460
474
461
475
return (
462
476
<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.
0 commit comments