2018-11-21
React Podcast Episode 29 - 'Don't Rewrite Your App for Hooks and Suspense' with Jared Palmer
react, shownote, podcast
react, shownote, podcast
_Photo by Kai Pilger on _Unsplash
More exciting features coming to React discussed in React Podcast episode #29.
Chantastic discussed Hooks & Suspense features with Jared Palmer's demo before React Conf 2018.
DISCLAIMER: Below is an unofficial show note highlighting discussions as I am not affiliated with podcast, just an avid listener :).
Sung's note: because class components would require you to access states using this & many new people suffer from not knowing to bind methods with current instance's this like this.method.bind(this)?
Less code is required
Learning curve could be an issue because this is a new feature thus requires you to remember "bunch of stuff".
But once you get over the hurdle it's great because
Jared is optimistic about Hooks' future and considers it the next logical step to take for React.
Suspense is about an "efficient loading of resources".
On fast connection, users prefer not to see a janky loading screens but on a slow connection they'd like to see it.
Speed, performance, and UX (User Experience) directly conflict with each other in this case because
Showing nothing still doesn't solve a problem as you still have to check of the loading state.
Many attempts were made to address the issue (lazy load data, assets, etc)
but they still don't react to (slow/fast) network.
Suspense allows you where to pause rendering while other components wait for resources.
When a resource arrives React can continue to render or else Suspense can show a fallback content.
Suspense literally stops time from happening.
Suspense enables us to orchestrate fallback declaratively.
Suspense can make loading dynamic components easy.
Use Error Boundaries.
Prerequisite is that your app needs to pass a strict mode by fixing all warning generated when you wrap your app using <React.StrictMode />.
Once your app passes the strict mode, you can replace ReactDOM.render with ReactDOM.createRoot, which you returns an object and call render on.
You can still use Concurrent Mode by
This enables an incremental adoption.
If Suspense is used outside concurrent mode, Suspense will fallback right away while child component is still loading itself.
You still get janks but with less code as it rids of loading state used in imperative code.
Benefits of using Suspense outside of Concurrent Mode are
Data fetching is simplified when combined with cache thanks to how Suspense works because everything (wasn't clear. Did Jared mean states? or components?) is always defined.
When everthing (?) is defined, TypeScript would not require partial classes (I need some feedback on this as I am a TS newbie...).
Coupling Hooks with Suspense + Cache, you can access asynchronous code as if it is a synchronous one providing a better readability.
This combination also solves waterfall issue of component state dependency orchestration (case where next components need to wait when previous ones are loaded).
Siblings of a single Suspense run in parallel.
https://gist.github.com/dance2die/a2ea846b6e5e89e157e6655854362993
In the code above, Component1 & 2 run in parallel.
Similar concept as how Promise.all runs promises in parallel.
Caveat: Nested components in Suspense are going to run with waterfall.
Sung's Note: Not sure how this works exactly...
And you can nest Suspense within each other infinitely.
It is just a reference implementation how it can hook into React.
Data normalization & validation features are missing (but coming).
Suspense is about efficiently orchestrating resources.
providing both good DX & UX
You can put anything (images, components, data, etc) in Cache
providing a better UX
React lets us describe "what" you want to do, not "how" you want to do.
Refer to the "declarative" loading states using Suspense.
Animation is the next challenge and JP considers it's an unsolved problem.
React team is making "right thing easy" to do with Suspense's maxDuration (the threshold time for the fallback).
It makes taking care of the 3rd state (network state where loading data takes long or short) between "loading" state and "data" state.
Incrementally adopt these new features, instead because React is always incrementally adoptable & improvable.
e.g.) Suspense can be used in concurrent mode for
Focus on movement not migration.
⚠ WARNING: Wait on this tip because data normalization & validation features are missing (but coming).
Jared has stressed this 4 times at