Replies: 2 comments 1 reply
-
Could you reproduce this as a CodeSandbox project? |
Beta Was this translation helpful? Give feedback.
1 reply
-
I managed to "bypass" this "issue" by refactor my code to: import useSWR, { type KeyedMutator } from 'swr';
import { useCallback } from 'react';
import BackendService from '@/services/backend';
const useBackend = <D, E = unknown>(path: string | null) => {
const { data, error, isLoading, mutate } = useSWR<D, E>(path, BackendService.get);
/**
* * Forced mutation is required for synchronization issues,
* * as noted here: https://stackoverflow.com/a/75796537/9105207
* * The second mutation call uses cache therefore not extra API call network is made
*/
const forcedMutation: KeyedMutator<D> = useCallback(
async (mutationData, options) => {
const data = await mutate(mutationData, options);
await mutate();
return data;
},
[mutate],
);
return { data, error, isLoading, mutate: forcedMutation };
};
export default useBackend; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Bug report
Description / Observed Behavior
I am using bounded mutation function. When I call the
mutate
fromuseSWR
, it won't update the cache. I validated it usingSWR DevTools
- when I called themutate
function - the cache did not update.I have a class with static methods. One of them is the so-called "fetcher" function.
This is the function I pass to
useSWR
hook.When I use
mutate
fromuseSWRConfig
, it works as expected.Expected Behavior
I expect to see immediate cache update once I call the bounded
mutate
function.Repro Steps / Code Example
I created my custom hook:
And this is my
BackendService
:Now, I have the following code:
So when the
onRevokeAllSecrets
is executed - the cached state does not change.But if I use
mutate
ofuseSWRConfig
it works:Could anyone tell why? I checked and my
BackendService.delete
call completes successfully.SWR version: 2.0.3
Beta Was this translation helpful? Give feedback.
All reactions