Getting Started with Suspense and Concurrent Rendering in React

Rate this content
Bookmark

React keeps on evolving and making hard things easier for the average developer.


One case, where React was not particularly hard but very repetitive, is working with AJAX request. There is always the trinity of loading, success and possible error states that had to be handled each time. But no more as the `<Suspense />` component makes life much easier.


Another case is performance of larger and complex applications. Usually React is fast enough but with a large application rendering components can conflict with user interactions. Concurrent rendering will, mostly automatically, take care of this.


You will learn all about using <Suspense />, showing loading indicators and handling errors. You will see how easy it is to get started with concurrent rendering. You will make suspense even more capable combining it with concurrent rendering, the `useTransition()` hook and the <SuspenseList /> component.

This workshop has been presented at React Summit 2020, check out the latest edition of this React Conference.

FAQ

The instructor for the React workshop is Maurice de Bayer, also known as the Problem Solver. He is a freelance developer and instructor.

The slides for the React workshop are available on GitHub, SlideShare, and Maurice de Bayer's website.

The main goals of the React workshop are to learn about using suspense and lazy loading, bundle splitting, using data resources to load data, and exploring concurrent rendering in React.

It is recommended to avoid copying and pasting code. Instead, type out the code, make mistakes, and fix them to learn and remember better.

The prerequisites for the React workshop include having Node.js and npm installed. The workshop is designed for experienced React developers.

React.lazy is used for bundle splitting in the workshop. It allows components to be loaded asynchronously, reducing the initial load time of the application.

React Suspense works by allowing components to suspend rendering while they wait for some asynchronous operation (like data fetching) to complete. It shows a fallback UI, like a loading spinner, until the operation is done.

Using data resources with React Suspense allows you to handle data fetching more efficiently by suspending the component until the data is available, making the code simpler and more maintainable.

Concurrent mode in React is an experimental feature that allows React to interrupt rendering work to handle more urgent updates, making applications more responsive.

New features available with React's concurrent mode include SuspenseList, useTransition, and more efficient interruptible rendering, which improve user experience and application performance.

Maurice de Beijer
Maurice de Beijer
125 min
15 Jul, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
The workshop video delves into various techniques to optimize React applications, focusing on React Concurrent Mode and Suspense. It starts with an introduction to React.lazy for lazy loading components, which helps in bundle splitting and reduces initial load times. The use of Suspense in React allows for suspending component rendering until data is loaded, enhancing user experience. The video also covers suspense boundaries, which help in managing loading states and errors. Concurrent Mode is discussed in-depth, highlighting its benefits in improving application responsiveness by allowing interruptions in rendering for user interactions. The concept of preloading data on user interactions, like hovering over links, is also explored to make applications appear faster. Other key points include handling errors with error boundaries and using experimental features like the SuspenseList and useTransition hook for better UI control.

1. Introduction to Workshop

Short description:

Welcome to this workshop on getting started with suspense and concurrent rendering and React. I'm Maurice de Bayer, a freelance developer and instructor. The goal of this workshop is to explore suspense, lazy loading, bundle splitting, and data resources. We'll also cover UI approaches to make applications appear faster and concurrent modes for more responsive applications. It's important to type the code instead of copying it for better learning. Prerequisites include Node, NPM, and cloning the repository.

So, let's get started, and if anyone else joins, they're not going to miss a lot because we're just starting with the intro. So welcome, everyone, welcome to this workshop on getting started with suspense and concurrent rendering and React. So who am I? I'm the guy who tries to forward the slides with the wrong focus. So that doesn't work. But now it does. So I'm Maurice de Bayer, also known as the Problem Solver. I'm a freelance developer and instructor, which kind of works nicely together. I teach what I do and I do what I teach. So it keeps me sharp.

Twitter handle is MauriceDB. So if you want to follow me. The slides are on several places, they're actually on the GitHub repo, which I'll share in a minute. They're already on SlideShare. They're on my website as well. It's kind of useful to keep them handy. There are lots of links in here to snippets of code or divs. You can follow along a bit easier than just remembering what I did and then trying to do the same. I do a lot of stuff, teach, develop, but I also publish the React Newsletter, which is a weekly newsletter, comes out every Wednesday afternoon, and has a bunch of articles about what's interesting about React and, well, React in a loose term, so everything related to React as well.

So about this workshop, the goal. We've basically got three main subjects. We're going to take a look at what we can do today. Use suspense, use lazy loading, bundle splitting, use data resources to load data. Then we're going to take a look at some UI approaches to make the application appear to be faster for the end user. It's actually not going to be faster because exactly the same happens, but we can make things happen in a different order so things actually appear faster, which is pretty nice. And suspense isn't a requirement for that, but it does make it a lot easier. And then the last bit is about concurrent modes, where we're going to do concurrent rendering. That's pretty much future stuff that's not possible with the current version of React. So we'll be using the experimental version of React to do that. In fact, we'll be using that all of the time, but you won't really be able to tell the difference until we get to that part. And with that, we'll be able to make applications more responsive. And we'll be able to orchestrate suspense boundaries using suspense list and transitions and things like that. So pretty nice. But keep in mind that's all very much in the future that's not available with the current released version of React. So we're kind of going from available use today to you can use this in the future.

Now a little tip. I'm providing you with all of the code you need. And you could just simply copy the code from the browser into Visual Studio code or whatever editor you're using. But in the end, you'll come away with an impression of what you did. And you've really honed your copy and paste skills. Copy and paste is an important skill. But if you want to learn something new, it's not the best way to learn it. So by actually typing it in, doing it, making mistakes, fixing those mistakes, you'll learn a lot better and you'll remember a lot better. So I would highly recommend not to copy too much.

So some prerequisites. I'm assuming everyone's good to go there. But just in case, you need to have Node installed because the React application we're going to use is built with create React app. So it's basically a nodes develop time stack. We need NPM to install packages, etc. Well, NPM ships with nodes by default, at least almost everywhere. Some Linux distros don't but I'm assuming everyone has that setup because this is for experienced React developers. So I'm kind of expecting all of that to be done. This is my setup at the moment. I've got nodes 12.18.3 and NPM 6.14.8. You don't need to have exactly the same versions like if you're on some version 12 of node, that's perfectly fine. Some version 10 will actually be fine as well. And the same with NPM, it doesn't need to be exactly this version but anything relatively recent will do. If you're on node six or something like that, yeah, then things won't work. But then any react stuff won't work. So that's a standard stuff.

The other thing you'll need is the repository and let me grab the repository and drag that over. Here it is. So this is basically as you can see from the bottom here done with create react app, a standard application. There is a source folder inside the source folder, there is a custom components folder with a bunch of components. I'll be showing lots of details about those later, and we'll be making lots of changes in that. So you need to clone that, click on this button, copy whatever is in here, use HTTPS or SSH, that doesn't really matter. So copy this and do a Gits clone somewhere on your disk so you get a copy of all of this. So the URL I'm going to show it again in the slides, but just in case you want to grab the entire community. So you can also scan this barcode or if you've already downloaded the slides so you can just click on it. It's actually a link and it will take you to repository as well, or if you scan the QR code, it will get you there as well. So once you've cloned that, open up a command window terminal window in that folder where it was clones.

Read also

2. Cloning Repository and Starting Application

Short description:

In this part, you'll learn how to clone the repository, install the dependencies, and start the application. You'll also explore the slides and links provided in the repository. Additionally, the breakout room feature of Zoom will be used to split participants into small groups for collaborative work. The first topic to be covered is using react.lazy and bundle splitting.

So in this case, for me, that's repos react suspense 2020 and execute an NPM CI or an NPM install there. Either will work, NPM CI slightly faster. That's why I prefer but NPM install will do exactly the same thing and it will install the dependencies shouldn't take very long. Took 8.7 seconds on my machine, but I've got a pretty fast machine. So pretty good. There.

Now, you'll see lots of slides like this as well during the workshop. There's code in there now. These are images. So it's kind of hard to copy the code from there. But they're also links. If you click on this, you get to the git diff, which is actually the cause of that change. So you see here, for instance, we had some import statements and they were commented out in this diff and they were replaced by react.lazy, which is actually pretty much the first change we're gonna do. That's why it's useful to have these around. You can click on the image, get to the diff.

Now, the slides are actually in here. They are in the repository. Only the not-so-nice thing with GitHub is that all of these are images. So I can scroll down to this same image and I can click on it, but nothing happens. Basically, what GitHub does is it renders an image for every page, which makes it perfectly but not interactive anymore. So it's recommended not to go there, but take this link instead. And I need to go here to click on it. It's exactly the same PDFs, but now they're hosted on my website and you can scroll down to the same image. Where is it here? And now I can click on it, and it does actually work. It is properly. Just to make life easier, you can also download it and open it some other way, but just make sure not to download it from the Git repository on GitHub because then the links don't work.

Another thing you'll see quite a bit is this guy, Jean Luc Picard's with Make It So, which is basically your cue to do stuff. I'm going to use the breakout room features of Zoom, so I'm basically going to split you up into a bunch of different breakout rooms with three or four people, so small groups so you can work together, ask each other questions, etc. This is basically the cue that's going to happen. I will try to keep the breakout rooms exactly the same. I've had some issues in the past with Zoom where it would forget about where a specific breakout room was, so I had to manually put them in, and I wasn't always sure which room that was. As a result of that, you might switch rooms, but barring that, I'll keep the same rooms with the same group of people so you can get used to working together as well, helping each other. With that out of the way, and actually I did not want to go there, that's actually kind go to the breakout rooms now. Make sure that you've got a working setup. So start the application. If you go to the shell you can do npm start, after you've cloned it and did the npm install. It should start up and basically show you a pretty simple application. You should be able to click on Movies. Click on Users, you'll see some users, and you see a loading spinner, you click on the You'll see some details and actually this is an interesting thing to see. You actually see two spinners there and they resolve always in the order where the user's favorite movie shows up first and then the user details. Not very important right now, but that will actually come up several times. So that should be working for you. So that shouldn't take very long, but let's go to the breakout room for a few minutes and then everyone can come back to the main room. I'll close the breakout rooms and we'll continue with the first step where we're actually going to do anything. Now, if you get stuck for some reason, anywhere along the process. Check with each other, try to help each other. If you can't, you can ask for help for me and I'll join you in the breakout room to help you out there as well. So let's see, I need to decrease the number of rooms a bit. So I'm going to break it out into seven different rooms. So there will be three or four of you in the same room. So I'm going to open them and give you a few minutes to make sure that the repository is cloned, NPM install is done, and it starts up properly. So I'll see you in a few minutes. So most people are back, not everyone. That actually closed a bit sooner than I was expecting, sorry about that. But apparently the breakout room was set to auto close in two minutes, which was not what I wanted. But I can't change it once it's opened, which is unfortunately in Zoom. But I'll change that before we open it again. So let me change the options right away. I do not want them to close automatically. So next time that won't happen. So hopefully you've had some time to clone the repository and at least do the npm installs. So let's start running. And while that's running, let's actually go and do the first thing. So the first thing we're going to take a look at is using react.lazy and bundle splitting. In order to see what's going on. I've got this application running but I'm actually going to stop it. And let's first editor here. I'm going to go to the package json. And I've got this analyze script which I added. That's not there by default.

Watch more workshops on topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured Workshop
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Next.js for React.js Developers
React Day Berlin 2023React Day Berlin 2023
157 min
Next.js for React.js Developers
Top Content
Featured WorkshopFree
Adrian Hajdin
Adrian Hajdin
In this advanced Next.js workshop, we will delve into key concepts and techniques that empower React.js developers to harness the full potential of Next.js. We will explore advanced topics and hands-on practices, equipping you with the skills needed to build high-performance web applications and make informed architectural decisions.
By the end of this workshop, you will be able to:1. Understand the benefits of React Server Components and their role in building interactive, server-rendered React applications.2. Differentiate between Edge and Node.js runtime in Next.js and know when to use each based on your project's requirements.3. Explore advanced Server-Side Rendering (SSR) techniques, including streaming, parallel vs. sequential fetching, and data synchronization.4. Implement caching strategies for enhanced performance and reduced server load in Next.js applications.5. Utilize React Actions to handle complex server mutation.6. Optimize your Next.js applications for SEO, social sharing, and overall performance to improve discoverability and user engagement.
Concurrent Rendering Adventures in React 18
React Advanced 2021React Advanced 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
Maurice de Beijer
Maurice de Beijer
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
React Hooks Tips Only the Pros Know
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured WorkshopFree
Maurice de Beijer
Maurice de Beijer
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
Introducing FlashList: Let's build a performant React Native list all together
React Advanced 2022React Advanced 2022
81 min
Introducing FlashList: Let's build a performant React Native list all together
Top Content
Featured Workshop
David Cortés Fulla
Marek Fořt
Talha Naqvi
3 authors
In this workshop you’ll learn why we created FlashList at Shopify and how you can use it in your code today. We will show you how to take a list that is not performant in FlatList and make it performant using FlashList with minimum effort. We will use tools like Flipper, our own benchmarking code, and teach you how the FlashList API can cover more complex use cases and still keep a top-notch performance.You will know:- Quick presentation about what FlashList, why we built, etc.- Migrating from FlatList to FlashList- Teaching how to write a performant list- Utilizing the tools provided by FlashList library (mainly the useBenchmark hook)- Using the Flipper plugins (flame graph, our lists profiler, UI & JS FPS profiler, etc.)- Optimizing performance of FlashList by using more advanced props like `getType`- 5-6 sample tasks where we’ll uncover and fix issues together- Q&A with Shopify team
React, TypeScript, and TDD
React Advanced 2021React Advanced 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced 2022React Advanced 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a web framework built on React Router that focuses on web fundamentals, accessibility, performance, and flexibility. It delivers real HTML and SEO benefits, and allows for automatic updating of meta tags and styles. It provides features like login functionality, session management, and error handling. Remix is a server-rendered framework that can enhance sites with JavaScript but doesn't require it for basic functionality. It aims to create quality HTML-driven documents and is flexible for use with different web technologies and stacks.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced 2023React Advanced 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
Using useEffect Effectively
React Advanced 2022React Advanced 2022
30 min
Using useEffect Effectively
Top Content
Today's Talk explores the use of the useEffect hook in React development, covering topics such as fetching data, handling race conditions and cleanup, and optimizing performance. It also discusses the correct use of useEffect in React 18, the distinction between Activity Effects and Action Effects, and the potential misuse of useEffect. The Talk highlights the benefits of using useQuery or SWR for data fetching, the problems with using useEffect for initializing global singletons, and the use of state machines for handling effects. The speaker also recommends exploring the beta React docs and using tools like the stately.ai editor for visualizing state machines.
Routing in React 18 and Beyond
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Routing in React 18 brings a native app-like user experience and allows applications to transition between different environments. React Router and Next.js have different approaches to routing, with React Router using component-based routing and Next.js using file system-based routing. React server components provide the primitives to address the disadvantages of multipage applications while maintaining the same user experience. Improving navigation and routing in React involves including loading UI, pre-rendering parts of the screen, and using server components for more performant experiences. Next.js and Remix are moving towards a converging solution by combining component-based routing with file system routing.
(Easier) Interactive Data Visualization in React
React Advanced 2021React Advanced 2021
27 min
(Easier) Interactive Data Visualization in React
Top Content
This Talk is about interactive data visualization in React using the Plot library. Plot is a high-level library that simplifies the process of visualizing data by providing key concepts and defaults for layout decisions. It can be integrated with React using hooks like useRef and useEffect. Plot allows for customization and supports features like sorting and adding additional marks. The Talk also discusses accessibility concerns, SSR support, and compares Plot to other libraries like D3 and Vega-Lite.