Hacker News new | ask | show | jobs
by strogonoff 564 days ago
> your usePartOfTheContext() would re-render every time the useContext() inside did, not helping with avoiding re-renders

If a hook returns the same value with a stable reference across renders, and it is passed as a prop to some downstream components, it does not matter whether the hook itself uses context or not: for downstream components, prop did not change and no render can be triggered.

1 comments

but downstream components would still need to be wrapped in memo to do avoid rendering if the prop did not change!
Thanks, I was not correct. Still, in my experience the impact from failing to ensure referential equality of props is usually a root cause of many issues.

If you make sure prop references are stable as early as possible, then if you run into poor performance you can always just wrap components in memo() (or some would just memo() all the things by default), but you may not even need it because renders also get cheaper when dependency diffing is effective and every hook does less work.

If prop references are unstable, things get messy in many ways.