Road to a Better UX with Suspense and Concurrent UI

Rate this content
Bookmark

Put up your thinking caps with Suspense and Concurrent mode! In this talk, I would discuss how to improve the existing user experience with the magic of React’s Suspense and Concurrent mode for non-blocked rendering. The talk would highlight the best practices and guidelines for the same.

This talk has been presented at React Summit Remote Edition 2021, check out the latest edition of this React Conference.

FAQ

React Suspense is an API that allows developers to control the loading experience of their web applications more finely by specifying loading states for different parts of the application, thereby improving user experience by managing asynchronous loading operations more effectively.

Concurrent Mode in React allows developers to prioritize and manage UI updates more efficiently. It enables the prefetching of data and rendering of components in the background, leading to smoother transitions and reducing the visibility of loading indicators, thus enhancing the user experience.

The useTransition hook in React provides a way to delay state updates that might cause a loading indicator to show. By using this hook, developers can avoid showing brief loading indicators for fast connections, making the application feel faster and more responsive.

Suspense and Concurrent Mode can be combined to optimize loading strategies in React applications. While Suspense handles the fallback loading UI for asynchronously loaded components, Concurrent Mode allows these components to be pre-fetched and rendered in the background, thus minimizing the user's perception of loading times.

As both Suspense for data fetching and Concurrent Mode are still experimental features, they can be prone to changes and might contain bugs. Developers are advised to experiment with these features in personal or side projects and to follow updates from the React team before implementing them in enterprise-level applications.

React Suspense allows developers to define specific boundaries within their application's UI where loading indicators should appear. This fine-grained control helps in maintaining a smooth user experience by only showing loaders where necessary and keeping the rest of the UI interactive.

You can follow Nikhil on Twitter and GitHub where he shares insights and discussions about design systems and performance optimizations. Engaging with him on these platforms can provide more direct access to his work and thoughts on these topics.

Nikhil Sharma
Nikhil Sharma
9 min
14 May, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
This talk explores how React Suspense and Concurrent mode can enhance the user experience by controlling loading areas, prefetching data, and reducing loading time. It demonstrates how UseTransition can remove unnecessary loaders by skipping the loading state if data arrives within a specified time. The speaker advises using these experimental features in personal projects rather than enterprise applications. The talk concludes with an invitation to join the journey at Postman and thanks to the organizers, sponsors, speakers, and audience for making the conference a success.

1. Introduction to React Suspense and Concurrent Mode

Short description:

In this talk, we'll discuss how to take UX to the next level with React Suspense and Concurrent mode. We'll cover controlling the loading area, prefetching data, and reducing loading time. We'll also explore how concurrent mode can remove unnecessary loaders. A demo of a Twitter dashboard will be used to showcase these concepts.

Hey everyone! So today, I'm actually going to be discussing how we can take UX to the next level with React Suspense and Concurrent mode. So before starting, for the ones who don't know me, hey, I'm Nikhil, I'm a software engineer at Postman. I actually handle stuff around design systems, Postman on the web, and Postman as a platform. You can actually find me on Twitter, on Github. I actually love to talk about design systems, and performance in general, so come say hi, it's actually going to be a great chat.

So before diving in, let's cover what are going to be the key takeaways of this lightning So in this talk, we are actually going to discuss how we can actually have more control over the loading area, which means how we can actually control where exactly do we want to show a loading shape. Is it going to be the whole page, or can we actually control loading on the UI that's going to be asynchronous with the API called Suspense? Secondly, we are going to see how we can start prefetching our data or our screens in background while we are right now in some other screen with the magic of concurrent mode. And thirdly, we'll actually see how we can actually reduce that janky spinner Nudra experience with actually the users for our faster connections actually face with a loading indicator which just comes and goes in a flash for them using a hook called use transition. Okay, so with that being said, let's actually deep dive and thread this road to a great UX together.

So I'll actually switch to a small demo of a Twitter dashboard that I actually have here, right? So this dashboard actually shows me a profile section where it shows my name, my designation, it has a small about me section, which actually displays a small description around me and the work that I've done. And there's a tweet section which actually just shows what is that then treat that I put it. Just don't try to go check the Twitter. It's just something that I couldn't write. So and there's an extra button, which I click and then I just see another tweet. Okay, so far, so good. Now, let me actually just go and switch to concurrent mode on version of this app. And they just see how we can actually improve the UX right now. So let me just refresh and I go to this about me section. And if you see I have this nice big loader on my whole tab. And because of like, even if I have just small content inside my tab, which is my description, which was like asynchronous, but I'm actually loading my whole tab, and I'm actually showing a screen up for this whole section, which is actually not a great user experience. Right? I could actually rendered my static content as regardless of the fact that my description is running or not right. So moving on to how it is right now coded, let's actually see how we are actually written this up in our code. So we have a suspense block inside this block, I have some static content as a small div. So what if I could actually take this particular div out and render this inside, outside the suspense block, right? And this is actually how suspense actually gives you a fine grain control by defining a small sort of a boundary, and it lets you control over what exactly do you want to show your loading upon. So let's say this, I refresh this app, I go to my about me section, and now my static content is already there for my users to interact with while my description was loading. Okay, so, so far so good and it's simple. Now, let's actually try to uncover and see how concurrent mode can actually help us in removing some of those more loaders, right? So on the left side, we actually have a synchronous synchronous mode of that app. And we actually, right now, fetch stuff while we are going to a screen, right? So in the left, and I click on this about me section, I see a nice loader, and then I start loading that stuff right. And, but since concurrent mode is something which switches tasks or into priority, I can actually start fetching my description and my tweets, actually, when I am right now on the profile page, and I can actually start rendering things in parallel, while I'm on this profile. So what do you think that what would happen if I just go to about me section and I just click on this, would I see a loader? Let's actually find out.

2. Removing Loaders with UseTransition

Short description:

So I click on a tweet section, and it starts fetching data in the background. The loader that appears when clicking the next tweet button is unnecessary for faster connection users. We can remove the loader using UseTransition and a timeout. This allows React to skip the loading state if the data arrives within a specified time. By using the start transition function, we can handle the loading state and use the spending billion value provided by UseTransition. This allows us to skip the loading state in synchronous concurrent mode and display the tweet instantly.

So I click on this, no loader, no spinner, it just appears that it was actually right now there, right? I click on this tweet section, but on this left-hand side, I click on this and it now starts to fetch stuff while I'm now on the stream, right?

Okay, so two spinners have now been mitigated. Okay, now let's start to actually uncover another use case. So what happens when I click on this next tweet button? I actually see a loader and it just comes and goes, right? I just see it in a flash. And this is something that our faster connection users face, right? And the purpose of this loader is not defeated because the small milliseconds of time would be something that you would actually skip that loading state, right? And you would actually just let your stream be as it is rather than just showing the loader.

Now, let's actually try to remove this loader itself and use a code called UseTransition. So I have this component right here, which is TweetList and let me just comment out this line, which is called UseTransition. And what it actually asks me to give it is a timeout in milliseconds. Now what this actually means is React will actually skip my loading state if my data actually comes within these two seconds of time. So if I check this onclick logic of my button, right, this is actually the logic where I actually set my state and enable loading state with suspense. Now what if I could actually just take all of this I copy and use start transition which actually has a function and I copy and paste all of that stuff inside. Okay. And the start function actually takes care of all the stuff that if the data actually arrives within these two seconds of time just to the loading state. Now let's actually use this spending billion value which use transition gives us. So let's disable this button to actually see this. I save it. Now let's actually see how things happen in synchronous concurrent mode. Right. So I click on this tweet section. Now I'm seeing a loading sheet for the first time. Click on this button. It's synchronous mode and I'm right now seeing a loader which is not really good. So let's actually see what happens when I switch to concurrent mode. So with that huge transition hook, I can actually skip that loading state now. Right. So I click on this next week. I don't see a loader. Just stay on it as it is. And then the tweet appears in a flash. Let's actually see one more time. I click on this no loader and the tweet appears just like a flash.

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.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
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 🤐)
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 Workshop
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.
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.
Web3 Workshop - Building Your First Dapp
React Advanced 2021React Advanced 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn