ReactJS – Does render get called any time “setState” is called?

Does React re-render all components and sub-components every time setState is called?

By default – yes.

There is a method boolean shouldComponentUpdate(object nextProps, object nextState), each component has this method and it’s responsible to determine “should component update (run render function)?” every time you change state or pass new props from parent component.

You can write your own implementation of shouldComponentUpdate method for your component, but default implementation always returns true – meaning always re-run render function.

Quote from official docs http://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate

By default, shouldComponentUpdate always returns true to prevent
subtle bugs when the state is mutated in place, but if you are careful to
always treat the state as immutable and to read-only from props and state
in render() then you can override shouldComponentUpdate with an
implementation that compares the old props and state to their
replacements.

Next part of your question:

If so, why? I thought the idea was that React only rendered as little as needed – when the state changed.

There are two steps of what we may call “render”:

  1. Virtual DOM renders: when render method is called it returns a new virtual dom structure of the component. As I mentioned before, this render method is called always when you call setState(), because shouldComponentUpdate always returns true by default. So, by default, there is no optimization here in React.

  2. Native DOM renders: React changes real DOM nodes in your browser only if they were changed in the Virtual DOM and as little as needed – this is that great React’s feature which optimizes real DOM mutation and makes React fast.

Leave a Comment