Skip to content

Commit 34997d3

Browse files
committed
Avoid scrolling
1 parent cfba52f commit 34997d3

File tree

1 file changed

+31
-22
lines changed
  • src/content/reference/react

1 file changed

+31
-22
lines changed

src/content/reference/react/use.md

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,9 @@ Calling `use` conditionally depending on whether a Promise is settled or not is
456456
import { Suspense, use, useState } from "react";
457457

458458
function preloadUser(id) {
459-
// This is not a real implementation of getting the Promise for the user.
460-
// A real implementation would probably call `fetch` or another data fetching method.
459+
// This is not a real implementation of getting the
460+
// Promise for the user. A real implementation would
461+
// probably call `fetch` or another data fetching method.
461462
// The actual implementation should cache the Promise.
462463
const promise = Promise.resolve(`User #${id}`);
463464

@@ -492,7 +493,7 @@ export default function App() {
492493
>
493494
Load User #2
494495
</button>
495-
<Suspense key={userId} fallback={<p>Loading user...</p>}>
496+
<Suspense key={userId} fallback={<p>Loading</p>}>
496497
{userUsable ? (
497498
<UserDetails userUsable={userUsable} />
498499
) : (
@@ -529,8 +530,9 @@ class PromiseWithStatus extends Promise {
529530
resolve = _resolve;
530531
reject = _reject;
531532
});
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
533+
// Setting the `status` field allows React to
534+
// synchronously read the value if the Promise
535+
// is already settled by the time the Promise is
534536
// passed to `use`.
535537
executor(
536538
(value) => {
@@ -548,11 +550,13 @@ class PromiseWithStatus extends Promise {
548550
}
549551

550552
function preloadUser(id) {
551-
// This is not a real implementation of getting the Promise for the user.
552-
// A real implementation would probably call `fetch` or another data fetching method.
553+
// This is not a real implementation of getting the
554+
// Promise for the user. A real implementation would
555+
// probably call `fetch` or another data fetching method.
553556
// The actual implementation should cache the Promise.
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
557+
// The important part is that we are using the
558+
// PromiseWithStatus subclass here. Check out the next
559+
// step if you're not controlling the Promise creation
556560
// (e.g. when `fetch` is used).
557561
const promise = PromiseWithStatus.resolve(`User #${id}`);
558562

@@ -573,8 +577,9 @@ export default function App() {
573577
<div>
574578
<button
575579
onClick={() => {
576-
// flushSync is only used for illustration purposes.
577-
// A real app would probably use startTransition instead.
580+
// flushSync is only used for illustration
581+
// purposes. A real app would probably use
582+
// startTransition instead.
578583
flushSync(() => {
579584
setUser(preloadUser(1));
580585
setUserId(1);
@@ -591,7 +596,7 @@ export default function App() {
591596
>
592597
Load User #2
593598
</button>
594-
<Suspense key={userId} fallback={<p>Loading user...</p>}>
599+
<Suspense key={userId} fallback={<p>Loading</p>}>
595600
{userUsable ? (
596601
<UserDetails userUsable={userUsable} />
597602
) : (
@@ -618,13 +623,15 @@ import { flushSync } from "react-dom";
618623

619624
function preloadUser(id) {
620625
const value = `User #${id}`;
621-
// This is not a real implementation of getting the Promise for the user.
622-
// A real implementation would probably call `fetch` or another data fetching method.
626+
// This is not a real implementation of getting the
627+
// Promise for the user. A real implementation would
628+
// probably call `fetch` or another data fetching method.
623629
// The actual implementation should cache the Promise.
624630
const promise = Promise.resolve(value);
625631

626632
// We don't need to create a custom subclass.
627-
// We can just set the necessary fields directly on the Promise.
633+
// We can just set the necessary fields directly on the
634+
// Promise.
628635
promise.status = "pending";
629636
promise.then(
630637
(value) => {
@@ -637,10 +644,11 @@ function preloadUser(id) {
637644
}
638645
);
639646

640-
// Setting the status in `.then` is too late if we want to create an already
641-
// settled Promise. We only included setting the fields in `.then` for
642-
// illustration purposes. Since our demo wants an already resolved Promise,
643-
// we set the necessary fields synchronously.
647+
// Setting the status in `.then` is too late if we want
648+
// to create an already settled Promise. We only included
649+
// setting the fields in `.then` for illustration
650+
// purposes. Since our demo wants an already resolved
651+
// Promise, we set the necessary fields synchronously.
644652
promise.status = "fulfilled";
645653
promise.value = value;
646654
return promise;
@@ -660,8 +668,9 @@ export default function App() {
660668
<div>
661669
<button
662670
onClick={() => {
663-
// flushSync is only used for illustration purposes.
664-
// A real app would probably use startTransition instead.
671+
// flushSync is only used for illustration
672+
// purposes. A real app would probably use
673+
// startTransition instead.
665674
flushSync(() => {
666675
setUser(preloadUser(1));
667676
setUserId(1);
@@ -680,7 +689,7 @@ export default function App() {
680689
>
681690
Load User #2
682691
</button>
683-
<Suspense key={userId} fallback={<p>Loading user...</p>}>
692+
<Suspense key={userId} fallback={<p>Loading</p>}>
684693
{userUsable ? (
685694
<UserDetails userUsable={userUsable} />
686695
) : (

0 commit comments

Comments
 (0)