If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
An important feature to know about React suspense is the concept of a suspense boundary. This allows you to easily consolidate multiple loading states into a single place, which prevents the cascade of loading indicators. This is what a cascade of loading looks like. We've all experienced websites where initially we wait for the page to load, then we wait for one section, for example, a table of contents to load. Then we wait for another section, for example, the video to load. Then we wait for another section, for example, the video description to load and so on. Suspense boundaries allow you to coordinate which parts
00:33
of your UI should always pop in together at the same Time. For example, we can wrap Our entire application into a single suspense component and show a single placeholder till all of the child components have finished their loading. You can even group Different portions of the UI into different boundaries to create the perfect user experience for your specific use case. For example, we can place a table of contents into its own suspense boundary and place the video and its description in a separate suspense boundary so the table of contents gets its own fallback, Whereas the video And its description are consolidated
01:07
into a single fallback with the theory out of the way. Let's demonstrate this with real code. Here we have a page layout with a nav on top, a table of contents on the left and on the right we have the lesson with the video component on top and the lesson description on the bottom. The code is straightforward As well. We have the page route that contains a nav, followed by a row containing the table of contents, along with the lesson component. And then the lesson component itself contains the lesson video along with the lesson description, the table of contents component, the lesson video component, and the lesson description component are going
01:41
to load the data that they require by using external APIs. This means that while we are still waiting for these components to finish rendering, the server cannot return the page to the client, which is why if you visit the website right now, we are stuck in a loading state for quite a while without any indication of what is happening till the final rendered page is returned without suspense. Maintaining the loading state within the individual components can get quite messy, but fortunately we already know suspense. So one thing that we can do is wrap each of the components that is taking a while to load in its own suspense boundary with a fallback specific to that component.
02:16
So now we have utilized three suspense components, one for the table of contents, one for the lesson video, and one for the lesson description. If you run this example, we can see the nav immediately and additionally, each component is wrapped in its own loading state. And once the component is done loading, it is seamlessly popped in place by suspense. Notice that even in this minimal example, thanks to suspense, there is no loading waterfall. All the three loading states are presented upfront immediately to the user. There is no loading, followed by more loading. From here, let's introduce a concept of a suspense boundary. Instead of wrapping the video
02:50
and the description in separate suspense components, we can provide a single suspense component that wraps them, providing a unified loading experience for the video and the description. So now if you run this example, we see a table of contents loading on the left and the lesson video along with this description on the right. Basically, suspense allows us to easily render a fallback UI if any of the child components are still loading. This makes it quite easy to build composite loading states, for example. Hey, we've done that for the video along with description, but we can even take it a step further. We remove the suspense entirely from the lesson component
03:23
and within the home component. We wrap the table of contents along with the lesson in a single suspense boundary, suspense will automatically go to this fallback UI if the table of contents is loading, or even children of the lesson, which are the video and the description are loading With this UI in place, if you visit the application again, we see the nav immediately and then we see a loading indicator. While that table of contents video and description are still pending, and only once they are done, we see the final fully rendered ui.