-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Reactive variables: Updating nested properties doesn't trigger a re-render #6699
Comments
So I tried updating the reactive var with Immer and it works. So are changes not detected when we use the spread(...) operator or the |
@ryands17 Can you show the code that issues/consumes the query (likely calling My only thought is that this code task.completed = !task.completed should perhaps be task = { ...task, completed: !task.completed } to ensure the |
I have a repo setup here that uses Immer for now. On this line in this method: If we convert the code in the method to this: let initialTasks = tasks()
let index = initialTasks.findIndex(task => task.id === id)
if (index !== -1) {
let newTasks = [...initialTasks]
newTasks[index].completed = !newTasks[index].completed
tasks(newTasks)
} The new state isn't reflected and the component doesn't re-render. So I guess shallow copying the variable doesn't work. |
That code still seems to be modifying properties of the |
Yes copying the particular task works well. |
Could it be there is an issue with reading reactive variables anyway? Using const updateVar = ({ dataVar, data }) => {
const cachedData = dataVar();
if (typeof cachedData === "object" && !Array.isArray(cachedData))
dataVar(
Object.keys(cachedData).reduce((acc, curr) => {
if (!data) acc[curr] = null;
else acc[curr] = data?.[curr] ? data[curr] : cachedData[curr];
return acc;
}, {})
);
else dataVar(data);
}; My variable has the following structure: export const myVar= makeVar({
fieldA: { propA: null, propB: "", propC: null },
fieldB: { propA: null, propB: "", propC: null },
fieldC: { propA: null, propB: "", propC: null },
fieldD: { propA: null, propB: "", propC: null },
fieldE: null
}); Updates of field A to D, while creating a new object, do not trigger an UI update this way. Am I missing something here? The stored value is properly updated and as said before triggers an update if |
I just tried using the current 3.2.0 beta and its |
@limekiln can you show your code, i have the same issue, i have tried to use useReactiveVar but it seems that i dont use it properly |
As it seems, update 3.3.7 did break the |
I am reasonably certain this is fixed by #8022. Feel free to ping and yell at me if this is not the case 🤗 Should be in a release soon. |
Shipped in 3.3.16 |
Intended outcome:
Following this blog post for working with reactive variables, updating a nested value in the reactive variable doesn't re-render the React app.
Here, the
addTask
method triggers a re-render, but thetoggleTask
method doesn't.Actual outcome:
The
toggleTask
method should trigger a re-renderVersions
System:
OS: macOS 10.15.6
Binaries:
Node: 12.18.2 - ~/.nvm/versions/node/v12.18.2/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.6 - ~/.nvm/versions/node/v12.18.2/bin/npm
Browsers:
Chrome: 84.0.4147.89
npmPackages:
@apollo/client: ^3.0.2 => 3.0.2
The text was updated successfully, but these errors were encountered: