-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useReactiveVar fails to trigger rerender #8681
Comments
@patrickshaughnessy The current implementation of export function useReactiveVar<T>(rv: ReactiveVar<T>): T {
const value = rv();
// We don't actually care what useState thinks the value of the variable
// is, so we take only the update function from the returned array.
const setValue = useState(value)[1];
// We subscribe to variable updates on initial mount and when the value has
// changed. This avoids a subtle bug in React.StrictMode where multiple
// listeners are added, leading to inconsistent updates.
useEffect(() => {
const probablySameValue = rv();
if (value !== probablySameValue) {
// If the value of rv has already changed, we don't need to listen for the
// next change, because we can report this change immediately.
setValue(probablySameValue);
} else {
return rv.onNextChange(setValue);
}
}, [value]);
return value;
} Can you double-check that you're not somehow using an outdated version of |
Hi @benjamn Thanks for the response. Yes - the second version ("broken one" for me) was just illustrating where in the history I observed the breakage
I created some dummy files similar to the code sandbox posted in the same repo (with the same node modules), and those also seem to be working as expected. At the moment, I still can't seem to figure out what could be happening on my end, but I guess that suggests a code issue that's likely unrelated to the useReactiveVar hook itself. We may have some proprietary modules doing something funky or maybe used some hooks wrong that's causing unexpected behavior (team is still new to hooks and Apollo). I think it'll be better to close this issue as I can't seem to reproduce. I'll reopen or just comment if I figure it out. Thanks for your time! |
Intended outcome:
I was playing around with
useReactiveVar
after upgrading to Apollo v3 in a large project (source code private unfortunately).I set up the Apollo cache and reactiveVars according to the docs:
Then in the component:
The component should have initially rendered the
value is: value
, then on button click, rerender withnewValue
.Actual outcome:
The React component was not rerendering after a button click. Checking logs in the button click handler, I found that the value of the reactive var was updated, but didn't trigger a rerender from React:
After searching around and trying a lot of different ideas, I finally found that the culprit seems to be the
useReactiveVar
hook.Going back through the hook's history, I found that my code worked fine and updated correctly when I substituted the original implementation of the hook in for the current version from apollo client:
This works: #6867
This is broken for me: #7652
How to reproduce the issue:
I've tried to reproduce in a code sandbox: https://codesandbox.io/s/pedantic-bush-e3fef?file=/src/App.js Seems to be working as expected though. I tried throwing in some other Apollo 2 things we're still doing like using local resolvers. I'm not really sure what could be the issue.
Honestly I'm not too sure if it's something on my end, something to do with the React version / Strict Mode (though I did try wrapping my component in StrictMode and it was the same behavior), or something else.
I figure if it's broken for a lot of people there'd be more noise about it, but I haven't been able to figure out why it's happening.
Versions
Running React v16
The text was updated successfully, but these errors were encountered: