-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
mutate in useSWRInfinite is broken when passing data and revalidate true #908
Comments
forgot to save my sandbox before sharing. it should be okay now |
I can reproduce the above. A further issue: if you use I found a workaround:
I take that this is updating the page cache forcefully and persist the change. |
@woxxy I have a similar issue. Can you better explain your workaround? When you mutate using I'm not understanding well how to mutate Thanks in advance |
for workaround you can do: mutate(data, false)
mutate() the first one mutates the cache, the second triggers revalidation. |
@pedro-pedrosa That workaround does not work for me. |
@woxxy Could you elaborate more on your workaround, specifically regarding "use the import mutate() to modify the key-cache"? Perhaps with a code example, would be awesome! |
If you want to revalidate all pages after mutating, you need to pass the const { mutate } = useSWRInfinite(getKey, fetcher, { revalidateAll: true })
mutate(pages) // ← this will first mutate the local data, and then revalidate all pages By default, SWR Infinite only revalidates the first page (there's a PR that fixes this behavior: #1301) that's because in most infinite loading cases, it's too expensive to revalidate everything. |
got connection refused every time i try to mutate data in swr infinite after updating the data / make a crud operation |
This bug fix somehow created a bug on Socketkit creating infinite loops using it with fallbackData. Here's the issue: #1638 (comment) |
this is how I also got it working. |
After hours of back and forth hustle this way around works but it revalidate all the pages. |
Not sure if anyone running on the same issue as me, but every google search I do on the subject leads me here, apparently using the bound
It may be possible another |
Bug report
Description / Observed Behavior
When using
useSWRInfinite
, if we call the boundmutate
function and we pass the data argument, the data is not updated or revalidated.Expected Behavior
I expect
useSWRInfinite
to both update the cache and re-fetch data from source.Repro Steps / Code Example
CodeSandbox
Notice how
mutate(data)
doesn't update the cache or revalidate the data butmutate(data, false)
does update the cache.mutate()
revalidates with no issues.Additional Context
SWR version.
0.4.0
I guess this is because the
mutate
function foruseSWRInfinite
is not updating the cached pages with thedata
parameter before callinguseSWR
mutate. When the infinite fetcher adapter runs, this is equal:config.compare(originalData[i], pageData)
.mutate
fromuseSWR
will just update the global cache key for the infinite list, not the individual pages as this code would assumeThe text was updated successfully, but these errors were encountered: