mutate is not rerendering
See original GitHub issueBug report
Description / Observed Behavior
I’m using mutate() to update data. Here is a little snip of what I do:
doRequest("someUrl", "PUT", someData).then(response => {
mutate({}, false);
mutate(response, false);
});
this way it is working and the data of my nextjs app is updated properly, but its pretty ugly since there are two re-renders triggered.
Here is what I want to do:
doRequest("someUrl", "PUT", someData).then(response => {
mutate(response, false);
});
or better would be
mutate(doRequest("someUrl", "PUT", someData), false);
This way data is not updated. I also tried to this:
doRequest("someUrl", "PUT", someData).then(response => {
mutate({...response}, false);
});
Changing mutate(response, false); to mutate({…response}, false); should not be nessesary since the object returned from server already is a new objectReference, but I gave it a try just to be sure.
Expected Behavior
I would expect swr to re-render every time when I use mutate with a new object reference.
Additional Context
SWR version: 0.3.4
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:13 (5 by maintainers)
Top Results From Across the Web
SWR not re-rendering when mutate is called - Stack Overflow
The problem is that the page isn't re-rendered, when I print the result of the mutate function, i see that the data has...
Read more >Mutation & Revalidation - SWR
mutate returns the results the data parameter has been resolved. The function passed to mutate will return an updated data which is used...
Read more >Refreshing Server-Side Props - Next.js - Josh W Comeau
It'll be initialized from the server-side props, but connect to the new API route for subsequent data-fetches. Use SWR to mutate the data...
Read more >When does React re-render components? - Felix Gerschau
React re-rendering explained and how to optimize React's re-render ... Directly mutating the props object is not allowed since this won't ...
Read more >Testing the cache side effects of a mutation? react apollo client
And I'm trying to expect on the place where the updated items would be, but it's not rerendering. The result from the mutation...
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 Free
Top 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
Having the same issue here
Having problems with
useSWRInfinite
and the bound mutate function. Trying to mutate an array of arrays, which does not throw an error and logs the updated data correctly in console, but doesn’t rerender the components. Anyone having the same problem?This works but with a UI lag (about 500 - 1000 ms)