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
+123-9Lines changed: 123 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -440,7 +440,7 @@ To use the Promise's <CodeStep step={1}>`catch`</CodeStep> method, call <CodeSte
440
440
441
441
If you are implementing a Suspense-enabled library, you can help React avoid unnecessarily suspending when you know the Promise has already settled, by using `status` and `value` or `reason` fields.
442
442
443
-
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)).
443
+
React will read the `status` field on the Promise to synchronously read the value without having to wait 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)).
444
444
445
445
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.
446
446
@@ -515,15 +515,116 @@ export default function App() {
515
515
516
516
```js src/App.js active
517
517
import { Suspense, use, useState } from"react";
518
+
import { flushSync } from"react-dom";
519
+
520
+
classPromiseWithStatusextendsPromise {
521
+
status ="pending";
522
+
value =null;
523
+
reason =null;
524
+
525
+
constructor(executor) {
526
+
let resolve;
527
+
let reject;
528
+
super((_resolve, _reject) => {
529
+
resolve = _resolve;
530
+
reject = _reject;
531
+
});
532
+
// Setting the `status` field allows React to synchronously read
533
+
// the value if the Promise is already settled by the time the Promise is
534
+
// passed to `use`.
535
+
executor(
536
+
(value) => {
537
+
this.status="fulfilled";
538
+
this.value= value;
539
+
resolve(value);
540
+
},
541
+
(reason) => {
542
+
this.status="rejected";
543
+
this.reason= reason;
544
+
reject(reason);
545
+
}
546
+
);
547
+
}
548
+
}
518
549
519
550
functionpreloadUser(id) {
520
551
// This is not a real implementation of getting the Promise for the user.
521
552
// A real implementation would probably call `fetch` or another data fetching method.
522
553
// The actual implementation should cache the Promise.
523
-
constpromise=Promise.resolve(`User #${id}`);
554
+
// The important part is that we are using the PromiseWithStatus subclass here.
555
+
// Check out the next step if you're not controlling the Promise creation
0 commit comments