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 with custom updater when no useSWR seems to have wrong state

See original GitHub issue

Bug report

Description / Observed Behavior

Simple case is:

  • a user action that will trigger a mutate on a key with a custom updater
  • after this action is done mount a component (or in a next js app push to router) that will use this key

-> the data that I see is not correct.

Expected Behavior

Expected behavior are:

  • if mutate is call and then a first useSWR is done on the same key, we should expect correct data -> request is correctly done, but data does not seems to be use / drop

Repo Steps / Code Example

if you run this example: click on the button you will see that request is done -> but only the mutate data is use.

https://codesandbox.io/s/clever-knuth-2z40o?file=/src/App.js:1128-1161

Additional Context

0.3.9

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
AyWacommented, Dec 20, 2020

No this is not what I want to see. Sorry if I didnt explain well. The behavior is:

  • I do not want to display the 2 component at the same time (because they could be different page) - > So the example given replicate that
  • I do not want to set the last argument to false (because I want to revalidate)

What I expect is:

  • user click button
  • mutate the key - > update data
  • mount a component that will use the key
  • so show fake
  • after api answers it should be updated

Does the flow is clear now? Let me know If my explanation is correct

0reactions
AyWacommented, Dec 20, 2020

Thanks a lot for the fast investigation and fix !! You Rock 🥳

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutation & Revalidation - SWR
The function passed to mutate will return an updated data which is used to update the corresponding cache value. If there is an...
Read more >
SWR not re-rendering when mutate is called - Stack Overflow
Whenever the url provided to useSWR changes, it will refetch the data also giving you its loading state. You only have to create...
Read more >
Data fetching in React and Next.js with useSWR to impress ...
Instead of fetching the data and storing it in state, we can store and update it directly in cache. This is where mutate...
Read more >
Understanding state management in Next.js - LogRocket Blog
A state is a JavaScript object that holds the current status of the data. You can think of it as a light switch...
Read more >
How to use the swr.mutate function in swr - Snyk
preventDefault() // mutate current data to optimistically update the UI // the fetch below could fail, in that case the UI will //...
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