-
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
Clear dedupingInterval of an unmounted key #828
Comments
Any workaround for this? It's really important to be able to cache some requests with long |
@lorenzomigliorero @smddzcy I made a demo https://codesandbox.io/s/relaxed-hooks-chjyi?file=/src/App.js. |
@MoonBall Because the prop is not |
I would like to fix it ASAP |
This problem is a duplicate of #751 You could update the cache data in this way mutate('/api/user', fetch('/api/user/edit', { method: 'post', name })) |
I think he was merely providing a workaround, not saying this isn't a bug. |
The global mutate function lives outside the react component but you could only access the global fetcher from the SWRConfigContext. So you have to tell mutate how you would like to change your cache. The global mutate function does two things
So if you call
Personally when i use swr for crud i would prefer Bound Mutate. In this way you might have batter DX because the typescript intellisense
funciton useUser() {
return useSWR('/api/user', fetcher)
}
function User() {
const { data } = useUser()
return <div>{data}</div>
}
function EditUser() {
// if you dont access data here this hook would not cause rerender here
// like a magic
const { mutate } = useUser()
const editName = async (name) => {
await fetch('/api/user/edit', { method: 'post', name })
mutate()
}
return <div>{data}</div>
} I hope this could help people who have similar problems. The global mutate function might be more suitable for prefetching or building some custom hooks like #1041 |
If I use bound mutates, I opt into renders/fetches in areas where I might not need it. I actually usually avoid bound mutates for this reason. @promer94 the problem is even if I set the 2nd argument, nothing changes if a relevant SWR hook isn't mounted... UNLESS the interval has passed. I think it would make sense to have mutate clear the interval so that the next time a relevant component mounts, it revalidates. |
@kylemh |
The Although there is no mounted hook, we can simply delete the previous request result. It seems like that the |
I ran into a bug making the repro 😅 codesandbox/codesandbox-client#5704 I'll get it to you as soon as I can. |
I have a better a design idea for |
I am currently just using a simple workaround, where I manually fetch the resource and directly insert it into cache like this: function useRefresh() {
const {cache, fetcher} = useSWRConfig()
return (key) => {
const shouldRefetch = !!cache.has(key)
if(shouldRefetch) {
return mutate(key, fetcher(key))
}
}
} Just use this hook, instead of What I wonder is why can't this behaviour be the default one for a simple |
@muqg If there's a global |
@shuding Right, but then we can just fallback to a regular What's more doing a However, when This I think illustrates the bigger issue with my expectations about what a Currently calling |
Thank you @muqg for taking time making that diagram, it's very helpful! I completely agree with this: And I've opened #1498 to address this bug. But my main concern is |
@shuding Agreed, |
Bug report
Description
I am developing a simple CRUD application with two views:
App.jsx
User.jsx
Edit.jsx
If the user follows these steps:
/api/user data contains an old cached value.
I've tried to manually delete the cache value instead of calling mutate, but the issue remains.
CONCURRENT_PROMISES remains fullfilled with the previous payload.
Expected Behavior
After calling mutate on Edit.jsx, I expect that a revalidation occurs when User.jsx is mounted, ignoring dedupingInterval value.
In other words, dedupingInterval should be cleared if mutate is called on an unmounted key.
The text was updated successfully, but these errors were encountered: