swr-simple-fetcher
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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,
    )
}

Package Sidebar

Install

npm i swr-simple-fetcher

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

7.71 kB

Total Files

6

Last publish

Collaborators

  • westbrookdaniel