I think the fact is that, when a parent component re-renders in React, in general, all the children components re-render as well.
I did one experiment to confirm:
https://codesandbox.io/s/currying-pine-r16rzi
return (
<div>
<div>Time now is {timeNow.toLocaleTimeString()}</div>
<TheTimeNow /><TheTimeNow />
</div>
);
This parent component re-renders, and <TheTimeNow />
has no change in props value (it has none), but it re-renders anyway and shows the updated time.
I think it is not the same to say, that React actually change the DOM, as I think the mechanism is that React use the previous virtual DOM to compare with the new virtual DOM, and update only the minimal actual DOM node as on document.documentElement
as needed.
This can be seen if you use Google Chrome and do Inspect Element on the time statement: only the time portion changes and the other English words stay the same. Compare to this where all the nodes changes if it is plain JavaScript: https://jsfiddle.net/8sj2ugae/1/ when you do Inspect Element.
However, can't we argue that, if a child is <Foo />
and since there is no props passed to it, then <Foo />
really should not have changed, and it is wasteful to call Foo()
? (if Foo
is a functional component, or call the render()
method if it is a class component.)
Now since all children re-renders, that means all their children also get re-rendered. Repeat this rule recursively, that would mean the whole subtree gets re-rendered.
My question is: is it true that they should not need to re-render if their props didn't change, or are there other factors that make them needing a re-render really?
React.memo
orshouldComponentUpdate
is used. – MediatizeuseMemo()
every where, it is really tight coupling. The team has to keep on thinking, oh this component we can useuseMemo()
but the other component cannot and this component now has to because reason XYZ makes it so... it is also very easy to have bugs this way, if the tight coupling itself isn't a problem – Unlicensed<Foo />
really needs no updating, logically speaking, I think. But it is like React just use a simple rule (and also a dumbed down rule) here, to update everything – Unlicensed