Continuous state updates with react-merge-refs
See original GitHub issueIf I understand correctly, the useInView
hook changes state — causing its parent component to render — when there is any change to the intersection entry. This can be a lot (e.g. while scrolling) and in some applications (e.g. WebGL rendered at 60fps, see https://docs.pmnd.rs/react-three-fiber/advanced/pitfalls) that is a substantial performance hit. For my application, I only need to make changes when boolean visibility changes from true to false or false to true, and the fractional state changes beyond that cause overhead. Is this an option that could be provided?
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Non referentially stable refs cause multiple updates #5 - GitHub
When a React element receives a different ref on re-renderer the previous ref is updated ... Continuous state updates with react-merge-refs ...
Read more >What happens when using this.setState multiple times in ...
React batches state updates that occur in event handlers and lifecycle methods. Thus, if you update state multiple times in a <div onClick...
Read more >How State Updates Are Merged in React | by Robin Kim
To recap, React.Component's setState( ) uses shallow merging to update state, ultimately saving us from listing out all of the keys and values...
Read more >Hooks FAQ - React
Is there something like forceUpdate? ... Both useState and useReducer Hooks bail out of updates if the next value is the same as...
Read more >How To Manage State with Hooks on React Components
Unlike class-based components, you cannot update several pieces of state with a single function call. Instead, you must call each function ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
See the FAQ: https://github.com/thebuilder/react-intersection-observer#how-can-i-assign-multiple-refs-to-a-component
react-merge-refs
has an open issue for the behavior you are seeing: https://github.com/gregberge/react-merge-refs/issues/5Here’s a simplified case reproducing the issue:
https://codesandbox.io/s/continuous-intersection-repro-o9ws74?file=/src/index.js
Current Behavior:
Renders
counter increases continuously.Expected Behavior:
Other notes:
The problem seems to be related to mixing
react-intersection-observer
withreact-merge-refs
. If I removereact-merge-refs
— which I’ll look into, but isn’t totally trivial in my real application — there’s no problem. Merging refs seems to be causing the intersection observer to re-bind itself.The example also includes a handmade
useInView
hook, which you can uncomment to test. It doesn’t render continuously (✅) and updates on intersection (✅) but doesn’t detect changes to the ref (⚠️).I’m not sure whether this is a problem in one package or the other, or just a misunderstanding of how react refs work — totally understand if this isn’t within the scope of
react-intersection-observer
. 🤔