question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Continuous state updates with react-merge-refs

See original GitHub issue

If 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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
thebuildercommented, Apr 18, 2022
0reactions
donmccurdycommented, Apr 18, 2022

Here’s a simplified case reproducing the issue:

https://codesandbox.io/s/continuous-intersection-repro-o9ws74?file=/src/index.js

Current Behavior:

  1. Open sandbox, notice that Renders counter increases continuously.
  2. Scroll down to the logo, notice that intersection event fires and font turns red.

Expected Behavior:

  1. Same as above, but without continuous rendering.

Other notes:

The problem seems to be related to mixing react-intersection-observer with react-merge-refs. If I remove react-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. 🤔

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found