JsPoint

A collection of essential articles for JavaScript, WebAssembly, TypeScript, Node.js, Deno, and Web development in general.

React v18 features: Suspense

Introduction to React v18 Suspense and Render-as-You-Fetch approach

Uday Hiwarale
JsPoint
Published in
17 min readApr 20, 2022

--

(source: unsplash.com)
(source: https://codesandbox.io/s/react-suspense-normal-fetch-parallel-5kiiuh)
(source: https://codesandbox.io/s/react-suspense-normal-fetch-waterfall-vyswny)
(source: https://codesandbox.io/s/react-suspense-normal-fetch-parent-state-management-emg2rq)

React v18 to the rescue

(source: https://codesandbox.io/s/react-suspense-lazy-load-wgnrjm)
<Suspense fallback={<p>Loading...</p>}>
<User name="John Doe" />
</Suspense>

The exact protocol a component should use to tell React that it’s suspended.

(source: https://gist.github.com/thatisuday/3f990e2133ced770369250cd3499368f)
(source: https://codesandbox.io/s/react-suspense-swr-1jpw8s)
<Suspense fallback={<p>Loading application...</p>}>
<User id="1" delay={5000} />
<Post id="1" delay={3000} />
</Suspense>
const { data, error } = useSWR(key, fetcher, {
suspense: true
});
(Chrome DevTools Networks Tab)
(source: https://codesandbox.io/s/react-suspense-swr-nested-2diurs)
<Suspense fallback={<p>Loading application...</p>}>
<UserWrapper>
<User id="2" delay={5000}/>
</UserWrapper>
<div>
<PostContainer />
</div>
</Suspense>
(Chrome DevTools Networks Tab)
(source: https://gist.github.com/thatisuday/6b2736545e8ab81bc2dfc93e0e7e7553)
(source: https://codesandbox.io/s/react-suspense-swr-suspense-boundary-lb9uve)
<Suspense fallback={<p>Loading application...</p>}>
<User id="1" delay={5000} />
<Post id="1" delay={3000} />
<Suspense fallback={<p>Loading photo...</p>}>
<Photo id="1" delay={10000} />
</Suspense>
</Suspense>
(Chrome DevTools Networks Tab)
(source: https://codesandbox.io/s/react-suspense-swr-re-suspend-7r1zwl)
<Suspense fallback={<p>Loading application...</p>}>
<User id="1" delay={5000} />
<Post id="1" delay={3000} />
<Suspense fallback={<p>Loading photo...</p>}>
<Photo id={photoId} delay={10000} />
</Suspense>
<button onClick={handlePhotoIdChange}>Load New Photo</button>
</Suspense>
(Chrome DevTools Networks Tab)
(source: https://codesandbox.io/s/react-suspense-swr-re-suspend-transition-tkbqo1)
const [isPending, startTransition] = useTransition();
const [isPending, startTransition] = useTransition();const handlePhotoChange = () => startTransition(() => {
const newPhotoId = Number(photoId) + 1;
setPhotoId(`${newPhotoId}`);
});
<Suspense fallback={<p>Loading application...</p>}>
<User id="1" delay={5000} />
<Post id="1" delay={3000} />
<Suspense fallback={<p>Loading photo with id {photoId}...</p>}>
<Photo id={photoId} delay={10000} />
</Suspense>
<button onClick={handlePhotoIdChange}>
{isPending
? `Loading New Photo with id ${photoId}`
: "Load New Photo"}
</button>
</Suspense>
const [photoId, setPhotoId] = useState("1");
const [loadingPhotoId, setLoadingPhotoId] = useState(photoId);
const [isPending, startTransition] = useTransition();const handlePhotoIdChange = () => {
const newPhotoId = Number(photoId) + 1;
setPhotoId(`${newPhotoId}`);
startTransition(() => {
setLoadingPhotoId(`${newPhotoId}`);
});
};
<Suspense fallback={<p>Loading photo with id {photoId}...</p>}
<Photo id={loadingPhotoId} delay={10000} />
</Suspense>
(source: https://codesandbox.io/s/react-suspense-swr-re-suspend-transition-fixed-cugvoh)
(Kausa Berlin officekausa.ai/about)

--

--

JsPoint
JsPoint

Published in JsPoint

A collection of essential articles for JavaScript, WebAssembly, TypeScript, Node.js, Deno, and Web development in general.