concerns about passing null as fetcher
See original GitHub issueBackground
@pacocoursey 's idea for SWR as a local cache is great.
const useSharedState = (key, initial) => {
const { data: state, mutate: setState } = useSWR(key, {
initialData: initial
})
return [state, setState]
}
But after #367 this won’t work anymore because window.fetch will be used when fn is undefined https://github.com/vercel/swr/blob/10e18cc9e6e607309db1aee81ac22b58690a76c3/src/use-swr.ts#L245-L248
However passing null as fetcher will work since we only replace the fetcher to window.fetch when fetcher is undefined.
useSWR(key, null ,{ initialData: initial })
But the typescript will complain this kind of usage. #730 fix this problem by accepting null as as the fetcher function.
Problem
- This usage is not well documented.
- passing null as the fetcher function is like a hack here to disable the whole stale-while-revalidate process in swr. I am not sure it is a good api design. I feel like it somehow leaks the implementation details.
- Passing null as the fetcher function makes most of the options useless here.
useSWR(key, null ,{ initialData: initial, /** most of options here will not be used since the revalidation is disabled here */ })
Possible alternatives
- a passive options and a different options type
useSWR(key, {
initialData: initial,
passive: true
/** typescript should complain about set options like onSuccess
which will only be used at stale-while-revalidate process */
})
- a new hooks share the caches
const { data: state, mutate: setState } = useSWRPassive(key, { initialData })
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:5 (1 by maintainers)
Top Results From Across the Web
fetch post always pass null to the parameters - Stack Overflow
I've tested the url and parameter in native android, it works there but here it doesn't take the parameter. It always sends the...
Read more >Null in JavaScript - GeeksforGeeks
Null is an object in JavaScript and represents primitive data types. ... If we pass null as the default parameter to any function...
Read more >Pass null argument with method overloading of String and ...
Tricky Interview Question : Pass null argument with method ... 24K views 1 year ago Tricky Java Programming Interview Questions - By Naveen ......
Read more >Pass NULL parameters to Stored Procedure ... - ASPSnippets
The following StoredProcedure accepts a NULL parameter @CustomerID and its value is used to fetch the Customer from the Customers table of the ......
Read more >Passing null values in SQL query to get all values - MSDN
This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions. Learn More · SQL Server Developer Center.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In SWR 1.0, there is no more default fetcher so you don’t need to pass
null
as fetcher anymore. Also we think thefallback
API is a better way to provide “initial data”. I think we can close this issue now 😃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
null
as a fetcher means.By passing
null
as the fetcher, any attempts to fetch data will return asnull
. This sounds obvious, but it’s important to remember that SWR can be configured to revalidate your data on focus and reconnect. If either of these events occur, the fetcher is called and your data is nownull
. Additionally, callingmutate
could also trigger a revalidation so it’s important to be mindful of this.Here’s my
useSharedState
hook based off of @shuding’s suggestion.In use, it looks like this.
As long as the fetcher is not called again, this will result in having shared state between hooks in a single page.