-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
concerns about passing null as fetcher #738
Comments
I'd like to drop my two cents regarding API design and passing null for fetcher, even while it's only slightly related. For this specific issue, it would mean instead of passing null for fetcher, you could just explicitly state that whatever the fetcher, it shouldn't be automatically invoked just because the hook was mounted. |
@promer94 I recent used the same approach but found it kind of buggy when using it as a shared hook with initialData several places with something like this: // use-navigation.js
const initialData = {
isOpen: false,
searchIsOpen: false,
searchHasItems: false,
level: 0,
levels: {}
}
const useNavigation = () => {
const { data: navState, mutate } = useSWR('navigation', null, {
initialData
})
const setNavState = newState => mutate({
...navState,
...newState
}, false)
return { navState, setNavState }
}
// Some other component
const { navState, setNavState } = useNavigation()
setNavState({ isOpen: !navState.isOpen})
// Shortly after somewhere else
setNavState({ searchHasItems: true}) The main issue was that when doing multiple I never found a fix for it, so i just assumed the API was not really ready for doing stuff like this, and like you mention, there is not a lot of documentation saying this something i should do 🙂 |
Instead using null as fetcher, you can give a try to:
But in this case, isValidating is stuck to true :/ so I do:
If someone has better solution, I'm interested. |
I've spent a little bit of time playing around with this, also inspired by @pacocoursey's blog post. His approach is a very eloquent and simple solution, but it is important to consider what using By passing Here's my import useSWR from 'swr'
export default function useSharedState(key, initialData) {
return useSWR(key, null, {
initialData,
revalidateOnFocus: false,
revalidateOnReconnect: false
})
} In use, it looks like this. const { data, mutate } = useSharedState('username', 'luke') As long as the fetcher is not called again, this will result in having shared state between hooks in a single page. |
In SWR 1.0, there is no more default fetcher so you don't need to pass |
Background
@pacocoursey 's idea for SWR as a local cache is great.
But after #367 this won't work anymore because window.fetch will be used when fn is undefined
swr/src/use-swr.ts
Lines 245 to 248 in 10e18cc
However passing null as fetcher will work since we only replace the fetcher to window.fetch when fetcher is undefined.
But the typescript will complain this kind of usage. #730 fix this problem by accepting null as as the fetcher function.
Problem
Possible alternatives
The text was updated successfully, but these errors were encountered: