-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Passing an updated fetcher method has no effect #1131
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
Comments
Changing |
I agree that a changed Passing the token as a |
➕1 on that. FWIW; I know several developers that didn't understand that the fetcher instance that was first used as the fetcher for that set of keys (and component?) will be used for all/most subsequent request (for that set of keys). Is it possible to add some information about this to the Data fetching page? It feels like 99% of the time when we pass a fetcher directly to |
This can't be true. We use local fetchers most of the time and something like this is very common in our code: export const useFetchJobTalents = (
params : FetchJobTalentsParams, query : FetchJobTalentsQuery, options
) : SWRResponse<FetchJobTalentsResult> => {
options = ... // omitted for brevity
const cacheKey = [fetchJobTalents.url, params.jobId, stringify(query)]
useSWR_GarbageCollection(cacheKey)
return useSWR(cacheKey, () => {
return fetchJobTalents(params, query) // custom fetcher
}, options)
} |
Also faced this as an issue, this is not a given reading the docs. |
Have also faced this issue, given the nature of hooks it would be assumed that if new instances of the fetcher would be used when the key changes. |
Would love to accept a PR to improve this! 👍 |
Bug report
Description / Observed Behavior
Passing an updated fetcher method to
useSWR
has no effect. The initial version of the fetcher will be used.I am passing a fetcher function to
useSWR
that is updated by applying a new access token.When I call
setAccessToken("valid-1")
at some point (and revalidate), the output will still be"fetch with token: valid-0"
.Expected Behavior
I was expecting swr to use the updated fetcher method. I believe this is the expected way when working with hooks in react.
In my example I'd expect
"fetch with token: valid-1"
.The workaround for now is to use
useRef
in combination withuseEffect
and then callfetcherRef.currect()
.If this behavior is intended, some words about this is the docs would be helpful and appreciated.
Repro Steps / Code Example
A simple example to reproduce and also the workaround using
useRef
:https://codesandbox.io/s/autumn-butterfly-c4xe2?file=/src/App.tsx
Additional Context
SWR version 0.5.5
Related: #515 #927
swr/src/use-swr.ts
Lines 479 to 486 in 3d380e2
The text was updated successfully, but these errors were encountered: