Re:Thinking in React Hooks
by erikpukinskis on Friday December 13, 2019 @02:42AM
(#21779827)
If you have limited props, why would your component be rendering often?
[ Parent
| on HN ]
Re:Thinking in React Hooks
by RussianCow on Friday December 13, 2019 @11:27AM
(#21782967)
Because the parent might render often.
Re:Thinking in React Hooks
by erikpukinskis on Friday December 13, 2019 @03:28PM
(#21785113)
That shouldn’t cause the child to re-render?
Re:Thinking in React Hooks
by RussianCow on Friday December 13, 2019 @07:56PM
(#21787046)
I'm not sure why you think it wouldn't. Any time a component renders, React also renders all of its children except the ones for which shouldComponentUpdate returns false. If that is defined by using PureComponent or React.memo on a child, then you have to make sure that the props you pass in are not going to change on every re-render (i.e. you can't pass an un-memoized callback because a new function reference will be created on every render).
I've created a CodePen to illustrate: https://codepen.io/RussianCow/pen/xxbERLL
Open the console and watch as the "render child" message is logged once per second, showing that the child is re-rendered every time the parent is.