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.

mutate is not rerendering

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

15reactions
michaeltroyacommented, Sep 24, 2020

Having the same issue here

5reactions
wobsorianocommented, Dec 12, 2020

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?

      mutate((currentData) => {
        return currentData.map((item) => {
          const posts = [...item.payload.posts];
          const postIndex = posts.findIndex((i) => i.post.guid === postId);

          if (postIndex !== -1) {
            posts[postIndex].post.total_likes = 1;
          }

          return {
            ...item,
            payload: {
              ...item.payload,
              posts,
            },
          };
        });
      }, false);

This works but with a UI lag (about 500 - 1000 ms)

      mutate((currentData) => {
        mutate(currentData.map((item) => {
          const posts = [...item.payload.posts];
          const postIndex = posts.findIndex((i) => i.post.guid === postId);

          if (postIndex !== -1) {
            posts[postIndex].post.total_likes = 1;
          }

          return {
            ...item,
            payload: {
              ...item.payload,
              posts,
            },
          };
        }))

       return currentData
      }, false);
Read more comments on GitHub >

github_iconTop 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 >

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