There are two helpers that can be used to add content while rendering:
...
const DisplayA = () => <div className={'containerA'}>
<button onClick={handleToggleA}>{"A toggled: " + toggledA.toString()}</button>
</div>
const displayB = () => <div className={'containerB'}>
<button onClick={handleToggleB}>{"B toggled: " + toggledB.toString()}</button>
</div>
return (
<>
<DisplayA />
{ displayB() }
</>
);
...
The problem is that in the first helper, React always discards the entire subtree and creates it again from scratch, as can be seen here:
I know, the first way is syntax sugar for React.createElement so a new component is created each render. However, the second way, a distinct arrow function is created each render too.
Why does React not know how to reuse the subtree the first way but does know the second way? What is happening under the hood?
How can we spot when the DOM subtree is discarded and recreated each render? Is it enough to assume that one should not create inline components, and use inline functions only?
Notice, that helpers can come from props, for example (render props pattern).
