SlideShare a Scribd company logo
1 of 57
Getting started with
Suspense &Concurrent
Rendering
Maurice de Beijer - @mauricedb
 Maurice de Beijer
 The Problem Solver
 Microsoft MVP
 Freelance developer/instructor
 Twitter: @mauricedb
 Web: http://www.TheProblemSolver.nl
 E-mail: maurice.de.beijer@gmail.com
2ABL - The Problem Solver
The React
Newsletter
© ABL - The Problem Solver 3
Workshop goal
 Use <Suspense />
 Bundle splitting and lazy loading
 Data resources
 UI approaches
 Fetch from render
 Fetching before render
 Concurrent mode
 Enabling Concurrent mode
 Orchestrating <Suspense /> boundaries using <SuspenseList />
 Using useTransition() to prioritize work
Type it out
by hand?
“Typing it drills it into your brain much better than
simply copying and pasting it.You're forming new
neuron pathways.Those pathways are going to
help you in the future. Help them out now!”
© ABL - The Problem Solver 5
Prerequisites
Install Node & NPM
Install the GitHub repository
© ABL - The Problem Solver 6
Install
Node.js & NPM
© ABL - The Problem Solver 7
Clone the
GitHub
Repository
© ABL - The Problem Solver 8
Install NPM
Packages
© ABL - The Problem Solver 9
Following
Along
 Repository: http://bit.ly/suspense-2020
 Slides: http://bit.ly/suspense-2020-pdf
© ABL - The Problem Solver 10
BundleSplitting &
Lazy Loading
Bundle
Splitting &
Lazy Loading
 By default CRA puts all application code in a single code bundle
 All code is loaded when the application first starts
 Many components may not be rendered until much later
 Or maybe never at all
 Splitting the code into multiple bundles makes the startup faster
 Only load additional parts when they are actually needed
 Using a bundle per route is a good start
 👉 Beware that shared code could end up in multiple bundles
 Import in the main bundle to really share the code
 Using React.lazy() automatically creates a new bundle
React.lazy()
<Suspense />
<Suspense />
 Allows React to “suspend” rendering a component subtree
 Used when a (grand) child component is not ready to be rendered
 ECMAScript bundle containing the component isn’t loaded yet
 The data needed for a component to render isn’t available yet
 The “fallback” component will be rendered instead
 Replaces the complete children component tree
 Rendering is suspended when a promise is thrown
 And resumed when the promise resolves
<Suspense />
Nesting <Suspense />
Nesting
<Suspense />
 Multiple suspense components can be nested
 React will use the closest parent <Suspense /> component
 Very useful to control what part of the UI is replaced by a fallback
Nesting
<Suspense />
Data Resources
Data
Resources
 Data resources are functions that
 Return data that is available
 Throw a promise when data is not available yet
 Resolve when that data becomes available
 Reject if there is some failure
 How the data is requested is not important
 Just an implementation detail
 Several data loading libraries already support this approach
 useSWR(), react-async and Relay
 Workshop createResource() based on gist by Ryan Florence
Creating a
Resource
Using a
Resource
Implement the
Resource
 Replace the useAbortableFetch() hook
and apply resource.read() in:
 .srccomponentsMovies.tsx
 .srccomponentsUsers.tsx
 .srccomponentsUserMovieDetails.tsx
 .srccomponentsUserUserDetails.tsx
<Suspense /> & Errors
<Suspense />
& Errors
 If a suspense resource fails to load an error is thrown
 When requesting it during the render()
 Catch the error using an ErrorBoundary
 Just like other runtime errors in React lifecycle functions
 Error boundaries can be nested
 Just like suspense boundaries
<Suspense />
& Errors
Parallel <Suspense />
Parallel
<Suspense />
 Multiple suspense boundaries can suspend in parallel
 React will suspend them all and show multiple fallback components
 If you want to render a component while others are still loading
 Multiple suspending components in a single <Suspense/> is also
fine
 Will resume when all resource promises are resolved
Parallel
<Suspense />
User Interface
Approaches
Fetch from
render
 Typically a component requests it’s data when it first renders
 Then displays a fallback component
 And re-renders when the data is available
Start fetching
before render
 Sometimes we can predict data requirements before render
 And start the request before the component is first rendered
 Possibly the data is already loaded when the component renders
 For example when a user hovers over a navigation link
 👉 Beware: Can lead to extra AJAX requests
 Not every hover is followed by a click on the link
Start fetching
before render
Start fetching
before render
Concurrent Mode
React
rendering
component
Use click event
Event running
Concurrent
mode
Event running
with
concurrent
mode
DanAbramov’s
2018 Demo
Concurrent
Mode
Feature
Comparison
Concurrent
Mode
 Legacy is the normal way of rendering a React application
 The only option that is fully supported
 Concurrent mode is the goal
 Only available in experimental builds of React
 Blocking mode is an intermediate mode
 Only available in experimental builds of React
 Use the experimental type definitions withTypeScript
 npm install react@experimental react-dom@experimental
Use the
Experimental
React
TypeScript
Definitions
Concurrent
Mode
 Use ReactDOM.createRoot() to create a root object
 And use it to render the React application
 Note: Still has the “unstable_” prefix
Using
createRoot()
Using <SuspenseList />
Orchestrating <Suspense /> boundaries
Using
<SuspenseList />
 <SuspenseList /> will let you control how multiple <Suspense />
components render their fallback
 The order in which child components show when ready
 If multiple child fallbacks components are displayed
Using
<SuspenseList />
Using useTransition()
To prioritize work
Using
useTransition()
 The useTransition() hook can be used to control how React
renders when components suspend
 Prevent the fallback component being rendered immediately
 The new components will be rendered when:
 Their resources are ready
 The timeout is expired
 The “old” UI can use the isPending state when rendering
Using
useTransition()
Conclusion
 You can use <Suspense /> today
 Suspend when lazily loading components
 Suspend when fetching data
 When not doing server side rendering
 Better UI approaches can make your application more responsive
 Start fetching data before you render the component
 It’s a trade off
 Concurrent mode
 Not recommended for production applications today
 Can make large applications more responsive
Maurice de Beijer
@mauricedb
maurice.de.beijer
@gmail.com
© ABL - The Problem Solver 57

More Related Content

What's hot

Java servlets
Java servletsJava servlets
Java servlets
lopjuan
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
vamsi krishna
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet
backdoor
 

What's hot (20)

Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Servlet
ServletServlet
Servlet
 
Java servlets
Java servletsJava servlets
Java servlets
 
Springboot2 postgresql-jpa-hibernate-crud-example with test
Springboot2 postgresql-jpa-hibernate-crud-example with testSpringboot2 postgresql-jpa-hibernate-crud-example with test
Springboot2 postgresql-jpa-hibernate-crud-example with test
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
 
Java servlets
Java servletsJava servlets
Java servlets
 
Simple Java component in Mule
Simple Java component in MuleSimple Java component in Mule
Simple Java component in Mule
 
Java EE 01-Servlets and Containers
Java EE 01-Servlets and ContainersJava EE 01-Servlets and Containers
Java EE 01-Servlets and Containers
 
Java Servlets & JSP
Java Servlets & JSPJava Servlets & JSP
Java Servlets & JSP
 
Java - Servlet - Mazenet Solution
Java - Servlet - Mazenet SolutionJava - Servlet - Mazenet Solution
Java - Servlet - Mazenet Solution
 
Jsp servlets
Jsp servletsJsp servlets
Jsp servlets
 
Spring boot
Spring bootSpring boot
Spring boot
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Exception handling in asp.net
Exception handling in asp.netException handling in asp.net
Exception handling in asp.net
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet
 
Servlets
ServletsServlets
Servlets
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Learn react-js
Learn react-jsLearn react-js
Learn react-js
 

Similar to Getting started with React Suspense and concurrent rendering

Beginning AngularJS
Beginning AngularJSBeginning AngularJS
Beginning AngularJS
Troy Miles
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
Niti Chotkaew
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
Julien Lecomte
 
Asp.net performance
Asp.net performanceAsp.net performance
Asp.net performance
Abhishek Sur
 

Similar to Getting started with React Suspense and concurrent rendering (20)

Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18
 
Express node js
Express node jsExpress node js
Express node js
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
Beginning AngularJS
Beginning AngularJSBeginning AngularJS
Beginning AngularJS
 
No Hugging, No Learning
No Hugging, No LearningNo Hugging, No Learning
No Hugging, No Learning
 
Scaling and Embracing Failure: Clustering Docker with Mesos
Scaling and Embracing Failure: Clustering Docker with MesosScaling and Embracing Failure: Clustering Docker with Mesos
Scaling and Embracing Failure: Clustering Docker with Mesos
 
Trustparency web doc spring 2.5 & hibernate
Trustparency web doc   spring 2.5 & hibernateTrustparency web doc   spring 2.5 & hibernate
Trustparency web doc spring 2.5 & hibernate
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
 
Spring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 IntegrationSpring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 Integration
 
Spring boot
Spring bootSpring boot
Spring boot
 
The Theory Of The Dom
The Theory Of The DomThe Theory Of The Dom
The Theory Of The Dom
 
Why meteor
Why meteorWhy meteor
Why meteor
 
EWD 3 Training Course Part 37: Building a React.js application with ewd-xpres...
EWD 3 Training Course Part 37: Building a React.js application with ewd-xpres...EWD 3 Training Course Part 37: Building a React.js application with ewd-xpres...
EWD 3 Training Course Part 37: Building a React.js application with ewd-xpres...
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
 
Asp.net performance
Asp.net performanceAsp.net performance
Asp.net performance
 
Owl: The New Odoo UI Framework
Owl: The New Odoo UI FrameworkOwl: The New Odoo UI Framework
Owl: The New Odoo UI Framework
 

More from Maurice De Beijer [MVP]

More from Maurice De Beijer [MVP] (20)

Practice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components AppPractice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components App
 
A foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software ProjectA foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software Project
 
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using CypressSurati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
 
Build reliable Svelte applications using Cypress
Build reliable Svelte applications using CypressBuild reliable Svelte applications using Cypress
Build reliable Svelte applications using Cypress
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureBuilding Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
 
Building reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and AzureBuilding reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and Azure
 
Building large and scalable mission critical applications with React
Building large and scalable mission critical applications with ReactBuilding large and scalable mission critical applications with React
Building large and scalable mission critical applications with React
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureBuilding Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
Building reliable web applications using Cypress
Building reliable web applications using CypressBuilding reliable web applications using Cypress
Building reliable web applications using Cypress
 
React suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred HitchcockReact suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred Hitchcock
 
From zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScriptFrom zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScript
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
The new React
The new React The new React
The new React
 
From zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScriptFrom zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScript
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
I am hooked on React
I am hooked on ReactI am hooked on React
I am hooked on React
 
Create flexible React applications using GraphQL apis
Create flexible React applications using GraphQL apisCreate flexible React applications using GraphQL apis
Create flexible React applications using GraphQL apis
 
Create flexible react applications using GraphQL API's
Create flexible react applications using GraphQL API'sCreate flexible react applications using GraphQL API's
Create flexible react applications using GraphQL API's
 
Docker for a .NET web developer
Docker for a .NET web developerDocker for a .NET web developer
Docker for a .NET web developer
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Getting started with React Suspense and concurrent rendering