Question: How useSWR cache depends on the fetcher? #600
-
I have 3 useSWR call: const a = useSWR('query', { fetcher: getDataA })
const b = useSWR('query', { fetcher: getDataB })
const c = useSWR('query', { fetcher: getDataC }) And all these three requests are revalidated by the same event. Then I found in our application, If I change to const a = useSWR('get-a', { fetcher: getDataA })
const b = useSWR('get-b', { fetcher: getDataB })
const c = useSWR('get-c', { fetcher: getDataC }) The problem is gone. I wonder if useSWR doesn't use the I use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
The cache doesn’t store the fetcher, it’s only a map (literally a Map) of key:data, if you use the same key with different fetchers only the last one will be stored in the cache. If the data is different then the key must be different, the key is the id of the data, what i recommend you to do is to create hooks like useA, useB, etc. And keep a key and a fetcher, this way you only call those hooks and can be sure both key and fetcher are always the same. |
Beta Was this translation helpful? Give feedback.
The cache doesn’t store the fetcher, it’s only a map (literally a Map) of key:data, if you use the same key with different fetchers only the last one will be stored in the cache.
If the data is different then the key must be different, the key is the id of the data, what i recommend you to do is to create hooks like useA, useB, etc. And keep a key and a fetcher, this way you only call those hooks and can be sure both key and fetcher are always the same.