When I have some children(HTML dom) as props to a child component and control in parent, I found that will trigger rerender!
Why React parent pass children will trigger rerender without state/props change?
How to avoid it? Check following.
const InsideChild = React.memo(({children}) => {
const countRef = useRef(0)
countRef.current += 1
return (
<div>render count: {countRef.current} {children}</div>
)
})
const OutsideParent = () => {
const [count, setCount] = useState(0)
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
<div>
Test1:
<InsideChild />
</div>
<div>
Test2:
<InsideChild>
<p>children as html dom will not trigger rerender.</p>
</InsideChild>
</div>
</div>
)
}
as sample code, Test1 will not trigger rerender, Test2 will. Is that possible to avoid it?
More detail and working sample here: