Replies: 3 comments 1 reply
-
export const useRecent = () => {
const {data, error} = useSWR(
`${config.apiUrl}/GetRecentFoundations`,
// Only when component is mounted, swr can have this function to do refetching
getThenResolve,
recentOptions
);
return {
recent: data,
isLoading: !error && !data,
isError: error
};
}
export const setRecent = async(fid) => {
await postThenResolve(
`${config.apiUrl}/SetFoundationRecent`,
{fid});
// what you do here is same as mutate(`${config.apiUrl}/GetRecentFoundations`, undefined, true)
// The cahce wont be update because the data is undefined
// when component is not mounted, swr wont refetch because it does not have the fetcher function
// when component is mounted, swr will refetch and update cache correctly
mutate(`${config.apiUrl}/GetRecentFoundations`);
} export const setRecent = async(fid) => {
await postThenResolve(
`${config.apiUrl}/SetFoundationRecent`,
{fid});
// get the fresh data
const data = getThenResolve(`${config.apiUrl}/GetRecentFoundations`)
// update the cache
mutate(`${config.apiUrl}/GetRecentFoundations`, data);
} |
Beta Was this translation helpful? Give feedback.
-
Thank you for the reply. I am going to try this tomorrow. |
Beta Was this translation helpful? Give feedback.
-
I ran into this issue while invalidating keys using the global mutate function. In my case, I could regenerate the cache key I wanted to invalidate, so I delete the entry from the cache before calling |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Greetings, I am having an issue with some unexpected behavior with regards to mutate(key). In my data fetching code, I have these hooks/functions:
I also have a component that fetches data with useRecent():
I use this pattern in two places currently, one where the component is mounted when the
setRecent()
occurs. The other, where the component is unmounted. When the component is mounted, everything works fine. Whenmutate()
is called, everything seems to refetch and rerender.However, when
setRecent()
is called when the component is unmounted, and I later return to it, I get stale data, and no refetch.I think I must be misunderstanding what
mutate(key)
does, or maybe I am unclear as to what thededupingInterval
is. I thought, that even with a high deduping interval, the mutate would cause the GetRecentFoundations cache to be invalid, and thus the next call touseSWR()
would require a revalidate, regardless if the SWR was mounted at the time of the mutate or not.What am I doing incorrectly?
Beta Was this translation helpful? Give feedback.
All reactions