How does React re-use child components / keep the state of child components when re-rendering the parent component?

createElement vs render vs mount

When a React Component such as your Button is rendered, a number of children are created with createElement. createElement(Timer, props, children) does not create an instance of the Timer component, or even render it, it only creates a "React element" which represents the fact that the component should be rendered.

When your Button is rendered, react will reconcile the result with the previous result, to decide what needs to be done with each child element:

  • If the element is not matched to one in the previous result, then a component instance is created then mounted then rendered (recursively applying this same process). Note that when Button is rendered for the first time, all of the children will be new (because there is no previous result to match against).
  • If the element is matched to one in the previous result, then the component instance is reused: its props are updated, then the component is re-rendered (again, recursively applying this same process). If the props did not change, React might even choose not to re-render as an efficiency.
  • Any elements in the previous result that was not matched to an element in the new result will be unmounted and destroyed.

React's diffing algorithm

An element "matches" another one if React compares them and they have the same type.

The default way for React to compare children, is to simply iterate over both lists of children at the same time, comparing the first elements with each other, then the second, etc.

If the children have keys, then each child in the new list is compared to the child in the old list that has the same key.

See the React Reconciliation Docs for a more detailed explanation.

Examples

Your Button always returns exactly one element: a button. So, when your Button re-renders, the button matches, and its DOM element is re-used, then the children of the button are compared.

The first child is always a Timer, so the type matches and the component instance is reused. The Timer props did not change, so React might re-render it (calling render on the instance with the same state), or it might not re-render it, leaving that part of the tree untouched. Both of these cases would result in the same result in your case - because you have no side-effects in render - and React deliberately leaves the decision of when to re-render as an implementation detail.

The second child is always the string "Clicks: " so react leaves that DOM element alone too.

If this.state.click has changed since the last render, then the third child will be a different string, maybe changing from "0" to "1", so the text node will be replaced in the DOM.


If Buttons render were to return a root element of a different type like so:

  render() {
    return createElement(this.state.clicks % 2 ? 'button' : 'a', { onClick: () => this.click() },
      createElement(Timer, null),
      'Clicks: ',
      this.state.clicks
    );
  }

then in the first step, the a would be compared to the button and because they are different types, the old element and all of its children would be removed from the DOM, unmounted, and destroyed. Then the new element would be created with no previous render result, and so a new Timer instance would be created with fresh state, and the timer would be back at 0.


Timer matches? previous tree new tree
no match <div><Timer /></div> <span><Timer /></span>
match <div>a <Timer /> a</div> <div>b <Timer /> b</div>
no match <div><Timer /></div> <div>first <Timer /></div>
match <div>{false}<Timer /></div> <div>first <Timer /></div>
match <div><Timer key="t" /></div> <div>first <Timer key="t" /></div>