swr-simple-fetcher
v1.1.0
Published
A simple fetcher for swr
Downloads
5
Maintainers
Readme
SWR Simple Fetcher
A simple fetcher for swr.
Install:
npm install swr-simple-fetcher
# or
yarn add swr-simple-fetcher
Simple usage:
const fetcher = createFetcher()
function useUsers() {
const { data, error, ...etc } = useSWR("/users", fetcher)
}
An example with a url builder.
The request is deduped based on the keys todoId
and userId
not the built url.
function buildUrl(todoId: string, userId: string) {
return `${baseUrl}/${userId}/todos/${todoId}`
}
const fetcher = createFetcher(buildUrl)
function useUserTodo(todoId: string, userId: string) {
const { data, error, ...etc } = useSWR<ResponseType, ErrorType>(
[todoId, userId],
fetcher,
)
}
An example with a custom requestor.
import { QueryClient } from "react-query"
const queryClient = new QueryClient()
function requestor(url: string) {
return queryClient.fetchQuery(url, queryFn)
}
function buildUrl(todoId: string) {
return `/todos/${todoId}`
}
const fetcher = createFetcher(buildUrl, requestor)
function useUserTodo() {
const { data, error, ...etc } = useSWR<ResponseType, ErrorType>(
[todoId, userId],
fetcher,
)
}