2. Thank you SeattleReactJS
@trivikram #SeattleReactJS 2
Oct’18: React Component State
vs Redux vs MobX vs Context
Dec’18: Will React Hooks
replace Redux?
4. What’s my name again?
@trivikram #SeattleReactJS 4
Tree Weak Rum
Tri vik ram
+ +
. .
5. 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
6. 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
7. 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
8. 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
9. 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
10. What is dynamic import?
const fileName = "./math.js";
import(fileName)
.then(m => m.add(16, 26));
@trivikram #SeattleReactJS 10
11. What is dynamic import?
(async () => {
const fileName = "./math.js";
const m = await import(fileName);
m.add(16, 26);
})();
@trivikram #SeattleReactJS 11
12. 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
18. 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.
34. When should I use Suspense?
@trivikram #SeattleReactJS 34
There is a single source of truth called
“it depends”
35. 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
36. Thank you for listening!
@trivikram #SeattleReactJS 36
Trivikram Kamat
trivikr@amazon.com
@trivikram
@trivikr
GitHub: https://github.com/trivikr/react-suspense-demo
Editor's Notes
Thanks SeattleReactJS organizers (Patrick, Tyler) for organizing these meetups.Thank SAP Concur for sponsoring location, food and drinks
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
Crack joke about creating Github URL for kids
Most people would be knowing this, but here’s a quick intro for those who are new
Warning: Your bundles will look way complicated than this!
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
Talk about async-await example coming next
SAY THAT we’re adding 16 and 26 so many times
Concentrate that we’re adding 15 and 27
YAWN YAWN YAWN!Is it because we’re not seeing this code in action, or is it because next slide is React.lazy()
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.
However, if you call lazily loaded component directly you’ll get this error
Explain how Menu and rest of the page is part of same chunk
Very few slides will tell you the solution
Understand the use cases of your application, play around with options, measure metrics and then decide.