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.

ReactiveVar doesn't cause hook to update if updated in a separate function

See original GitHub issue

Hello! I was working with reactiveVars, following along with https://youtu.be/xASrlg9rmR4?t=957 the issue may be that I, like the video, was creating a reactiveVar in the cache, rather than just on its own. But here is what I ran into: I have my reactive var, and I made a function to update that reactive var (like the video demonstrates with delete-todo)

i have a button that, when clicked, calls that function to update the reactive var, and i display the value of that var with useReactiveVar

however it doesn’t update

Intended outcome: when i call the addColor function, it should update the reactiveVar, and that should in turn cause a re-render because my page refers to the value changed by it

Actual outcome: calling addColor does seem to update the var, but that doesn’t trigger a re-render on the useReactiveVar hook

How to reproduce the issue: https://codesandbox.io/s/use-reactive-var-no-update-nh75m?file=/src/App.tsx

cacheModel.ts

export const cacheMoney: InMemoryCache = new InMemoryCache({
 /// other cache settings
});
export const initialUserSettingsVar = cacheMoney.makeVar<UserSettings>(initialUserSettings);
export function addColor(userSettingsVar: ReactiveVar<UserSettings>) {
  return (newColor = 'green') => {
    //get latest value of the settings
    const currVal = userSettingsVar();
    //update the color
    currVal.favoriteColor = newColor;
    //set the new value
    userSettingsVar(currVal);
  };
}

MyComponent.tsx

export const QueryPage = () => {
  const otherSettingsData = useReactiveVar(initialUserSettingsVar);

return (
 <>
      <Button
        onClick={() => {
          addColor(initialUserSettingsVar)(); //why doesn't this work?
          // initialUserSettingsVar({ ...initialUserSettingsVar(), favoriteColor: 'blue' }); //this works
        }}
      >
        Add Color
      </Button>
<div>Fav Color: {otherSettingsData.favoriteColor} </div>
)

}

Versions apollo-client “latest” (yarn lock says 3.3.11)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
madmaxlaxcommented, May 12, 2021

@benjamn btw, we’ve been using reactiveVars on our team for a few months now and they’re AWESOME way easier than redux and even easier than React Context. yall should be shouting about these on the rooftops. even for folks not even calling a GraphQL backend

3reactions
benjamncommented, Mar 3, 2021

@madmaxlax The root value that you store in the reactive variable has to change for any notifications to happen, so this code

    const currVal = userSettingsVar();
    //update the color
    currVal.favoriteColor = newColor;
    //set the new value
    userSettingsVar(currVal);

needs to modify currVal somehow, rather than mutating its contents:

userSettingsVar({
  ...userSettingsVar(),
  favoriteColor: newColor,
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactive variables: Updating nested properties doesn't trigger ...
So I tried updating the reactive var with Immer and it works. So are changes not detected when we use the spread(...) operator...
Read more >
Reactive variables - Apollo GraphQL Docs
New in Apollo Client 3, reactive variables are a useful mechanism for representing local state outside of the Apollo Client cache. Because they're...
Read more >
variable in react hook is not updated - Stack Overflow
In the following example I try to pose my problem: When I use classes and enter the value of 1, it returns a...
Read more >
Why Your Vue Component Isn't Updating (and how to fix it)
If we're using the options API in Vue 2 or Vue 3, we need to put the variable in the data function: export...
Read more >
Reactive Variables In GraphQL Apollo Client
In the above code, we import myReactiveVariable and we update it by calling the variable and placing the new value inside of it....
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