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
+82Lines changed: 82 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -436,6 +436,88 @@ To use the Promise's <CodeStep step={1}>`catch`</CodeStep> method, call <CodeSte
436
436
437
437
---
438
438
439
+
### Avoiding fallbacks by passing Promise subclasses {/*avoiding-fallbacks-by-passing-promise-subclasses*/}
440
+
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
+
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.
444
+
445
+
<Sandpack>
446
+
447
+
```js src/App.js active
448
+
import { Suspense, use, useState } from"react";
449
+
import { preload } from"./data-fetching.js";
450
+
451
+
functionUserDetails({ userUsable }) {
452
+
constuser=use(userUsable);
453
+
return<p>Hello, {user}!</p>;
454
+
}
455
+
456
+
exportdefaultfunctionApp() {
457
+
const [userId, setUserId] =useState(null);
458
+
// The initial
459
+
const [userUsable, setUser] =useState(null);
460
+
461
+
return (
462
+
<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