This is improved version, not the original one.
Because many of the designs of swr are very stubborn. I have some different opinions, so I made a new version of swr. If you thing this is my stubborn, you can ignore it.
- Not caching
- Must provide first arguments
key
to operate the cache
import useSWR from 'swr'
const fetcherA = () => fetch('/api/user')
function PageA() {
const { data, error, isLoading } = useSWR(fetcherA)
if (error) return <div>failed to load</div>
if (isLoading) return <div>loading...</div>
return <div>hello {data.name}!</div>
}
In this example, the React Hook useSWR
accepts only fetcher
function.
It's different from the original swr, I don't wanna you provide key
, fetcher.toString()
is enough
const fetcherB = (id) => fetch(`/api/user/${id}`)
function PageB() {
const { id } = useParams()
const { data, error, isLoading } = useSWR(id, (id) => fetcherB(id))
// ...
}
If your fetcher function wanna some custom arguments, you can provide it as the first argument.
The MIT License.