SlideShare a Scribd company logo
1 of 36
React Suspense for people in a hurry
@trivikram #SeattleReactJS 1
Thank you SeattleReactJS
@trivikram #SeattleReactJS 2
Oct’18: React Component State
vs Redux vs MobX vs Context
Dec’18: Will React Hooks
replace Redux?
Trivikram Kamat
@trivikram #SeattleReactJS 3
Software Development Engineer
trivikr@amazon.com
@trivikram
@trivikr
What’s my name again?
@trivikram #SeattleReactJS 4
Tree Weak Rum
Tri vik ram
+ +
. .
What is this talk going to cover?
•Bundling, Code Splitting and import
•React Suspense and lazy()
•Go through an example app
@trivikram #SeattleReactJS 5
Thank YouTuber Harry Wolff whose tutorials helped me prepare this talk
What is bundling?
•Merging multiple JavaScript files into a
single file called “bundle”
•Tools like Webpack, Browserify, Parcel
etc do it for you
@trivikram #SeattleReactJS 6
Bundling example
@trivikram #SeattleReactJS 7
// math.js
export function add(a, b) {
return a + b;
}
// app.js
import { add } from “./math.js”
console.log(add(16, 26)); //42
// bundle.js
function add(a, b) {
return a + b;
}
console.log(add(16, 26)); //42
What is Code Splitting?
• Bundling is great, but as your app grows, bundle will
grow too - especially if you include large 3p libraries
• To avoid that, we’ve to now “split” the bundle 😜
• Code Splitting – creates multiple bundles for you which
can dynamically loaded at runtime
• Lazy-loading – reduce the amount of code needed during
the initial load, “lazy-load” other things if/when required.
@trivikram #SeattleReactJS 8
What is non-dynamic (static) import?
• import { add } from “./math.js”
• You must specify what to import/export at compile time
• The import declaration can only appear at the top level of a module,
so you can’t import modules inside other statements
• The module specifier ”./math.js” cannot be computed at runtime
@trivikram #SeattleReactJS 9
What is dynamic import?
const fileName = "./math.js";
import(fileName)
.then(m => m.add(16, 26));
@trivikram #SeattleReactJS 10
What is dynamic import?
(async () => {
const fileName = "./math.js";
const m = await import(fileName);
m.add(16, 26);
})();
@trivikram #SeattleReactJS 11
Dynamic import is Stage-3 proposal
• Note that dynamic import is a TC39 Stage-3 proposal
(not a standard yet) https://github.com/tc39/proposal-
dynamic-import
• However, it’s supported in Chrome, Safari, Firefox,
TypeScript, babel, NodeJS (10+)
@trivikram #SeattleReactJS 12
Dynamic import benefits
Benefit 1: loading on demand
button.addEventListener("click",
() => {
import("./math.js")
.then (m => m.add(16, 26));
});
@trivikram #SeattleReactJS 13
Dynamic import benefits
Benefit 2: loading based on conditions
if (someCondition()) {
import("./math.js")
.then (m => m.add(15, 27));
}
@trivikram #SeattleReactJS 14
Dynamic import benefits
Benefit 3: compute module specifiers
import(`./${mathModule}.js`)
.then(m => m.add(14, 28));
@trivikram #SeattleReactJS 15
React.lazy() 😴
lets you render a dynamic import as a regular component
@trivikram #SeattleReactJS 16
Fallback UI
@trivikram #SeattleReactJS 17
Suspense 🤔
@trivikram #SeattleReactJS 18
If the module containing Bar component is not yet loaded by the time
Foo component gets rendered, then we must show some fallback
content - like Loading indicator. That’s what Suspense does.
Suspense 🤔
@trivikram #SeattleReactJS 19
Demo!
@trivikram #SeattleReactJS 20
Demo!
@trivikram #SeattleReactJS 21
Demo!
@trivikram #SeattleReactJS 22
Demo!
@trivikram #SeattleReactJS 23
Demo!
@trivikram #SeattleReactJS 24
Demo!
@trivikram #SeattleReactJS 25
Demo!
@trivikram #SeattleReactJS 26
Demo!
@trivikram #SeattleReactJS 27
Demo!
@trivikram #SeattleReactJS 28
Demo!
@trivikram #SeattleReactJS 29
Demo!
@trivikram #SeattleReactJS 30
Demo!
@trivikram #SeattleReactJS 31
Demo!
@trivikram #SeattleReactJS 32
High res image loading
Low res image available
Demo!
@trivikram #SeattleReactJS 33
When should I use Suspense?
@trivikram #SeattleReactJS 34
There is a single source of truth called
“it depends”
Summary
@trivikram #SeattleReactJS 35
• We learnt about
• Bundling – merging multiple JavaScript files into a single “bundle”
• Code-Splitting – creating multiple bundles which can be
dynamically loaded at runtime
• Dynamic import – import only when required
• React
• React.lazy() – render a dynamic import as a regular component
• Suspense – show fallback content for React.lazy component
• We went through a demo to see how all this works
Thank you for listening!
@trivikram #SeattleReactJS 36
Trivikram Kamat
trivikr@amazon.com
@trivikram
@trivikr
GitHub: https://github.com/trivikr/react-suspense-demo

More Related Content

What's hot

Security: The Value of SBOMs
Security: The Value of SBOMsSecurity: The Value of SBOMs
Security: The Value of SBOMs
Weaveworks
 
The Power of GitOps with Flux & GitOps Toolkit
The Power of GitOps with Flux & GitOps ToolkitThe Power of GitOps with Flux & GitOps Toolkit
The Power of GitOps with Flux & GitOps Toolkit
Weaveworks
 
Timed Text At Netflix
Timed Text At NetflixTimed Text At Netflix
Timed Text At Netflix
Rohit Puri
 

What's hot (20)

C# development workflow @ criteo
C# development workflow @ criteoC# development workflow @ criteo
C# development workflow @ criteo
 
Painless container management with Container Engine and Kubernetes
Painless container management with Container Engine and KubernetesPainless container management with Container Engine and Kubernetes
Painless container management with Container Engine and Kubernetes
 
Webhooks do's and dont's: what we learned after integrating +100 APIs - Giuli...
Webhooks do's and dont's: what we learned after integrating +100 APIs - Giuli...Webhooks do's and dont's: what we learned after integrating +100 APIs - Giuli...
Webhooks do's and dont's: what we learned after integrating +100 APIs - Giuli...
 
Blazing fast sites using Blaze, Hybrid CMS NYC
Blazing fast sites using Blaze, Hybrid CMS NYCBlazing fast sites using Blaze, Hybrid CMS NYC
Blazing fast sites using Blaze, Hybrid CMS NYC
 
Why reinvent the wheel at Criteo?
Why reinvent the wheel at Criteo? Why reinvent the wheel at Criteo?
Why reinvent the wheel at Criteo?
 
Security: The Value of SBOMs
Security: The Value of SBOMsSecurity: The Value of SBOMs
Security: The Value of SBOMs
 
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017Introduction to gRPC - Mete Atamel - Codemotion Rome 2017
Introduction to gRPC - Mete Atamel - Codemotion Rome 2017
 
DevOps Toolkit
DevOps ToolkitDevOps Toolkit
DevOps Toolkit
 
Monitoring Weave Cloud with Prometheus
Monitoring Weave Cloud with PrometheusMonitoring Weave Cloud with Prometheus
Monitoring Weave Cloud with Prometheus
 
Red hat forum istio & kiali - introduction and overview
Red hat forum   istio & kiali - introduction and overviewRed hat forum   istio & kiali - introduction and overview
Red hat forum istio & kiali - introduction and overview
 
Building a Distributed Build System at Google Scale
Building a Distributed Build System at Google ScaleBuilding a Distributed Build System at Google Scale
Building a Distributed Build System at Google Scale
 
Overview of modern software ecosystem for big data analysis
Overview of modern software ecosystem for big data analysisOverview of modern software ecosystem for big data analysis
Overview of modern software ecosystem for big data analysis
 
Loki: An Opensource Zipkin/Prometheus Mashup written in Go.
Loki: An Opensource Zipkin/Prometheus Mashup written in Go.Loki: An Opensource Zipkin/Prometheus Mashup written in Go.
Loki: An Opensource Zipkin/Prometheus Mashup written in Go.
 
Cloud foundry history
Cloud foundry historyCloud foundry history
Cloud foundry history
 
Google cloud functions
Google cloud functionsGoogle cloud functions
Google cloud functions
 
Triangle Devops Meetup 10/2015
Triangle Devops Meetup 10/2015Triangle Devops Meetup 10/2015
Triangle Devops Meetup 10/2015
 
The Power of GitOps with Flux & GitOps Toolkit
The Power of GitOps with Flux & GitOps ToolkitThe Power of GitOps with Flux & GitOps Toolkit
The Power of GitOps with Flux & GitOps Toolkit
 
Netflix and Containers: Not A Stranger Thing
Netflix and Containers:  Not A Stranger ThingNetflix and Containers:  Not A Stranger Thing
Netflix and Containers: Not A Stranger Thing
 
Cloudsolutionday 2016: Docker & FAAS at getvero.com
Cloudsolutionday 2016: Docker & FAAS at getvero.comCloudsolutionday 2016: Docker & FAAS at getvero.com
Cloudsolutionday 2016: Docker & FAAS at getvero.com
 
Timed Text At Netflix
Timed Text At NetflixTimed Text At Netflix
Timed Text At Netflix
 

Similar to react-suspense-for-people-in-hurry

Reactive & Realtime Web Applications with TurboGears2
Reactive & Realtime Web Applications with TurboGears2Reactive & Realtime Web Applications with TurboGears2
Reactive & Realtime Web Applications with TurboGears2
Alessandro Molina
 

Similar to react-suspense-for-people-in-hurry (20)

Front end microservices - October 2019
Front end microservices - October 2019Front end microservices - October 2019
Front end microservices - October 2019
 
Android Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyondAndroid Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyond
 
Cleaning your architecture with android architecture components
Cleaning your architecture with android architecture componentsCleaning your architecture with android architecture components
Cleaning your architecture with android architecture components
 
React js
React jsReact js
React js
 
Get together on getting more out of typescript & angular 2
Get together on getting more out of typescript & angular 2Get together on getting more out of typescript & angular 2
Get together on getting more out of typescript & angular 2
 
Microsoft & Open Source - a 'brave new world' - ProgSCon 2017
Microsoft & Open Source - a 'brave new world' - ProgSCon 2017Microsoft & Open Source - a 'brave new world' - ProgSCon 2017
Microsoft & Open Source - a 'brave new world' - ProgSCon 2017
 
MongoDB.local Atlanta: MongoDB Stitch Tutorial
MongoDB.local Atlanta: MongoDB Stitch TutorialMongoDB.local Atlanta: MongoDB Stitch Tutorial
MongoDB.local Atlanta: MongoDB Stitch Tutorial
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom components
 
How to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
How to build an ETL pipeline with Apache Beam on Google Cloud DataflowHow to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
How to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
 
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
 
Spring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsugSpring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsug
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API's
 
The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015
 
Clean Architecture @ Taxibeat
Clean Architecture @ TaxibeatClean Architecture @ Taxibeat
Clean Architecture @ Taxibeat
 
Reactive & Realtime Web Applications with TurboGears2
Reactive & Realtime Web Applications with TurboGears2Reactive & Realtime Web Applications with TurboGears2
Reactive & Realtime Web Applications with TurboGears2
 
Using and contributing to the next Guice
Using and contributing to the next GuiceUsing and contributing to the next Guice
Using and contributing to the next Guice
 
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...
 
UniRx - Reactive Extensions for Unity(EN)
UniRx - Reactive Extensions for Unity(EN)UniRx - Reactive Extensions for Unity(EN)
UniRx - Reactive Extensions for Unity(EN)
 
Presentation Tier optimizations
Presentation Tier optimizationsPresentation Tier optimizations
Presentation Tier optimizations
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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...
 
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...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
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...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

react-suspense-for-people-in-hurry

Editor's Notes

  1. Thanks SeattleReactJS organizers (Patrick, Tyler) for organizing these meetups. Thank SAP Concur for sponsoring location, food and drinks
  2. Thank you Seattle React JS for giving opportunities to speak If you see these talks, and compare them to today’s talk - you’ll realize how much I’ve improved
  3. Crack joke about creating Github URL for kids
  4. Most people would be knowing this, but here’s a quick intro for those who are new
  5. Warning: Your bundles will look way complicated than this!
  6. Why large bundle is bad? Because it’ll affect performance (load time) While you haven’t reduced the overall amount of code in your app, you’ve avoided loading code that the user may never need Explain that we’ll revisit Code Splitting in the demo
  7. Talk about async-await example coming next
  8. SAY THAT we’re adding 16 and 26 so many times
  9. Concentrate that we’re adding 15 and 27
  10. YAWN YAWN YAWN! Is it because we’re not seeing this code in action, or is it because next slide is React.lazy()
  11. React will load bundle containing Bar component after Foo component gets rendered. React.lazy takes a function that must call a dynamic import(). This must return a Promise which resolves to a module with a default export containing a React component.
  12. However, if you call lazily loaded component directly you’ll get this error
  13. Explain how Menu and rest of the page is part of same chunk
  14. Very few slides will tell you the solution Understand the use cases of your application, play around with options, measure metrics and then decide.