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.

useReactiveVar unreliable with fast changing primitive

See original GitHub issue

i’m using a reactive var to track rollover state as a boolean, and I’ve noticed that useReactiveVar is unreliable if the state updates quickly (which is often the case with rollovers).

const rVar = makeVar(true);

const Demo = () => {
  const val = useReactiveVar(rVar);
  console.log(val);

  return (
    <div
      style={{ marginTop: 200, border: `1px solid red` }}
      onMouseEnter={() => {
        console.log(`enter`, rVar());
        if (!rVar()) rVar(true);
      }}
      onMouseLeave={() => {
        console.log(`leave`, rVar());
        if (rVar()) rVar(false);
      }}
    >
      val: {val ? `true` : `false`}
    </div>
  );
};

In this simplified demo, if the user rolls over the div quickly and repeatedly for awhile, the component will eventually no longer rerender, despite the boolean value in the reactive var updating.

An apparent workaround is to use store the state in a non-primitive like {val:true} which seems to reliably trigger a rerender

Versions ver 3.3.9

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
benjamncommented, Feb 16, 2021

@dontsave Can you try making this reproduction runnable, using this template? I tried, but I couldn’t get the component to stop updating, using 3.3.9 or any later version.

Other questions that might be relevant:

  1. What exact versions of react and react-dom are you using?
  2. What browser version?
0reactions
brainkimcommented, Apr 30, 2021

Shipped in 3.3.16

Read more comments on GitHub >

github_iconTop Results From Across the Web

The state of useReactiveVar tutorials is poor - Apollo GraphQL
This solution helped: spread the variable to a new object so the new reference triggers a state change, and thus, cascades a new...
Read more >
Apollo reactive variable are not working when imported into ...
If using the reactiveVar directly it WILL NOT update when the value changes, it'll only update on initial render.
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