Thinking in synchronisation instead of mount and unmount removes a whole class of bugs. 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.

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.
Putting it into a component
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.
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.
Tim Berners-Lee, W3C Web Accessibility Initiative launch, 1997
Quick takeaway
Effects synchronise; they do not initialise.
Common mistakes
Derive, don’t store
Compute values from props and state during render.
Synchronise
Effects connect React to the outside world.
Stream
Send HTML as soon as each part is ready.
Server rendering and streaming add a second axis: where the component runs. Keep data fetching on the server when you can, and ship only the interactive pieces to the browser.
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.
Wrapping up
We have been running this approach for several months now. The initial setup took about a day, and the ongoing cost is close to zero once the team knows where to look.
Comments 0