useReactiveVar unreliable with fast changing primitive
See original GitHub issuei’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:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
@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:
react
andreact-dom
are you using?Shipped in 3.3.16