React Suspense 🔀
Improving UX with a faster, more predictable app.
⚠️ Warning ⚠️
This workshop material deals with EXPERIMENTAL features in React. Please do not copy/paste any of the code you find here into a production application and expect it to work. Even when the features are released they may not work the same as demonstrated in this workshop material.
That said, the concepts in this workshop will very likely be applicable when these features are stable, so enjoy the workshop!
Pre-Workshop Instructions/Requirements
In order for us to maximize our efforts during the workshop, please complete the following things to prepare.
-
📺 only necessary if the workshop is remote via Zoom -
👋 specific to the material for this workshop -
Setup the project (follow the setup instructions below) (~5 minutes)
-
📺 Install and setup Zoom on the computer you will be using (~5 minutes) -
📺 Watch Use Zoom for KCD Workshops (~8 minutes). -
Watch Setup and Logistics for KCD Workshops (~24 minutes). Please do NOT skip this step.
-
Watch Dan Abramov's talk Beyond React 16 | JSConf Iceland 2018 (33 minutes)
-
Go through my Learn React Hooks Workshop, or have the equivalent basic experience of using hooks. You should be experienced with
useState,useEffect, anduseRef. -
Go through my Advanced React Hooks Workshop, or have the equivalent experience. You should be experienced with
useContextanduseReducer(experience withuseMemoanduseCallbackis a bonus).
The more prepared you are for the workshop, the better it will go for you.
Workshop Outline
Here are the concepts we'll be covering:
- Opting into React Concurrent Mode
- Thinking in Suspense
- The fundamentals of "suspending"
- Structuring
<React.Suspense />components with fallbacks - Using
useTransition - Refactor an existing async interaction to suspense
- The difference between the three data-fetching approaches:
- Fetch-on-Render (not using Suspense)
- Fetch-Then-Render (not using Suspense)
- Render-as-You-Fetch (using Suspense)
- Using
<React.SuspenseList />to coordinate multiple suspending components
System Requirements
All of these must be available in your PATH. To verify things are set up
properly, you can run this:
git --version
node --version
npm --versionIf you have trouble with any of these, learn more about the PATH environment variable and how to fix it here for windows or mac/linux.
Setup
After you've made sure to have the correct things (and versions) installed, you should be able to just run a few commands to get set up:
git clone https://github.com/kentcdodds/react-suspense.git
cd react-suspense
npm run setup --silent
This may take a few minutes. It will ask you for your email. This is optional and just automatically adds your email to the links in the project to make filling out some forms easier If you get any errors, please read through them and see if you can find out what the problem is. If you can't work it out on your own then please file an issue and provide all the output from the commands you ran (even if it's a lot).
You may be able to work through the entire workshop in the browser. Go to this codesandbox and you should be good to go. Note that sometimes people have trouble with codesandbox not working quite right with tests, but you should be able to work around that. If you're concerned, then it would probably be better to just set things up locally.
Running the app
To get the app up and running (and really see if it worked), run:
npm startThis should start up your browser. If you're familiar, this is a standard react-scripts application.
You can also open the deployment of the app on Netlify.
Running the tests
npm testThis will start Jest in watch mode. Read the output and play around with it. The tests are there to help you reach the final version, however sometimes you can accomplish the task and the tests still fail if you implement things differently than I do in my solution, so don't look to them as a complete authority.
Workshop Outline
React Suspense Workshop
🔀
🏡 Utah👩 👧 👦 👦 👦 🐕 🏢 kentcdodds.com🐦 /🐙 @kentcdodds🏆 testingjavascript.com🥚 kcd.im/egghead🥋 kcd.im/fem💌 kcd.im/news📝 kcd.im/blog📺 kcd.im/devtips💻 kcd.im/coding📽 kcd.im/youtube🎙 kcd.im/3-mins❓ kcd.im/ama
Schedule
😴 Logistics💪 Simple Data-fetching😴 10 Minutes💪 Render as you fetch💪 useTransition for improved loading states🌮 30 Minutes💪 Cache resources💪 Suspense Image😴 10 Minutes💪 Suspense with a custom hook💪 Coordinate Suspending components with SuspenseList❓ Q&A
Questions
Please do ask! Interrupt me. If you have an unrelated question, please ask on my AMA.
Zoom (for remote workshops)
- Help us make this more human by keeping your video on if possible
- Keep microphone muted unless speaking
- Breakout rooms
Exercises
src/exercise/00.md: Background, Exercise Instructions, Extra Creditsrc/exercise/00.js: Exercise with Emoji helperssrc/__tests__/00.js: Testssrc/final/00.js: Final versionsrc/final/00.extra-0.js: Final version of extra credit
The purpose of the exercise is not for you to work through all the material. It's intended to get your brain thinking about the right questions to ask me as I walk through the material.
Helpful Emoji 🐨 💪 🏁 💰 💯 🦉 📜 💣 👨💼 🚨
Each exercise has comments in it to help you get through the exercise. These fun emoji characters are here to help you.
- Kody the Koala Bear
🐨 will tell you when there's something specific you should do - Matthew the Muscle
💪 will indicate what you're working with an exercise - Chuck the Checkered Flag
🏁 will indicate that you're working with a final version - Marty the Money Bag
💰 will give you specific tips (and sometimes code) along the way - Hannah the Hundred
💯 will give you extra challenges you can do if you finish the exercises early. - Olivia the Owl
🦉 will give you useful tidbits/best practice notes and a link for elaboration and feedback. - Dominic the Document
📜 will give you links to useful documentation - Berry the Bomb
💣 will be hanging around anywhere you need to blow stuff up (delete code) - Peter the Product Manager
👨💼 helps us know what our users want - Alfred the Alert
🚨 will occasionally show up in the test failures with potential explanations for why the tests are failing.
Disclaimers
- React Concurrent Mode is experimental
- I've never shipped Concurrent Mode to production, and you shouldn't either (yet)
- React Suspense is a particularly primitive API and we're still working out good abstractions for it
- I've been informed that the API for suspending will change before the stable release (don't worry though, the concepts are solid)
- I will probably say "I don't know" as a response to your questions sometimes
Workshop Feedback
Each exercise has an Elaboration and Feedback link. Please fill that out after the exercise and instruction.
At the end of the workshop, please go to this URL to give overall feedback. Thank you! https://kcd.im/rs-ws-feedback
Contributors
Thanks goes to these wonderful people (emoji key):
Kent C. Dodds |
Filipp Riabchun |
lauchness |
This project follows the all-contributors specification. Contributions of any kind welcome!