React setState, what really happens under the hood?
Javier Machin

May 18, 2019

React setState, what really happens under the hood?

Unraveling the intricate process behind React's state updates and lifecycle methods

engineeringreact

Hello, code enjoyers! Today, we're pulling back the curtain on one of React's most fundamental features: setState.
Ever wondered what actually happens when you call this magical function?


The setState Trigger

Picture this: you've just called setState in your React component. It seems simple on the surface, but behind the scenes, it sets off a chain reaction of events that would make Rube Goldberg proud!


When you call setState, React doesn't immediately update the state. Instead, it creates what we call a 'pending state update'. Think of it as React taking a sticky note and jotting down your state change for later.


The Reconciliation Process

Now, imagine React as a meticulous librarian. Once it has collected all the 'sticky notes' (state updates), it starts a process called reconciliation. This is where the real magic happens!


React creates a new virtual DOM tree with your updated state. Then, it compares this new tree with the current one, identifying the minimal number of changes needed to update the actual DOM. It's like playing an intense game of 'spot the difference'!


Batching: React's Efficiency Hack

Here's where React gets really clever. Instead of updating the DOM for each setState call, it batches multiple updates together. It's like doing all your grocery shopping in one trip instead of making separate trips for each item.

  • This batching optimizes performance by reducing the number of DOM manipulations.
  • It ensures that your component doesn't re-render unnecessarily.

The Lifecycle Dance

During this process, React calls several lifecycle methods in a specific order:

  1. getDerivedStateFromProps: Called after a component is instantiated or when it receives new props.
  2. shouldComponentUpdate: Determines if the component should re-render.
  3. render: Creates the new Virtual DOM.
  4. getSnapshotBeforeUpdate: Captures information from the DOM before changes are made.
  5. React updates DOM and refs
  6. componentDidUpdate: Called after the update is committed to the DOM.

For functional components using hooks, the process is similar but uses the useEffect hook instead of these lifecycle methods.


The Final Update

Once React has gone through all these steps:

  • It updates the actual DOM in one go.
  • It calls the final lifecycle method, componentDidUpdate.
  • Finally, it updates the state object of your component.

And voila! Your component now reflects the new state, all thanks to the intricate dance choreographed by React's setState mechanism.


Why This Matters

Understanding this process helps you:

  • Write more efficient React code
  • Debug state-related issues more effectively
  • Appreciate why we should treat state as immutable
  • Optimize your use of lifecycle methods

So the next time you call setState, take a moment to appreciate the machinery working behind the scenes to keep your UI snappy! 🚀

Service Workers vs Web Workers, What's the difference?

Discover their superpowers and when to use them

Sep 25, 2020Read more