Watch a page render in chunks and see where Suspense boundaries fall. React keeps changing how it wants us to think about rendering, and each change removes a class of bugs if you adopt the new model fully instead of translating old habits.
Sample footage: Sintel, an open movie by the Blender Foundation, released under a Creative Commons licence.
The mental model
Think of a component as a description of what the screen should look like for the current data. Effects synchronise that description with things outside React, such as timers, subscriptions, or the document title.
- Derive values during render instead of storing them.
- Give every effect a cleanup function.
- Keep state as close to where it is used as possible.
- Put Suspense boundaries where users expect a loading state.
The mistakes we see most often are effects that compute data which could have been derived during render, and state that duplicates props. Both lead to screens that are briefly wrong after every update.
Questions, corrections, and war stories are welcome in the comments. Several of our best follow-up posts started as a reader pointing out something we had missed.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Martin Fowler, Refactoring, 1999
Counterpoint: for small teams the simpler approach in the first section is usually enough.
The quote in the middle is doing a lot of work here, and it earns it.
Is there a repo with the full example? Happy to open a PR with a TypeScript version.
Agreed. Start simple and only reach for this when you feel the pain.
Tried it on a side project over the weekend and it worked first time, which never happens.
Solid write-up. The warning about caching bit us hard last year.