Skip to content

apades/swr

 
 

Repository files navigation

SWR

This is improved version, not the original one.

Introduction

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.

  1. Not caching
  2. Must provide first arguments key to operate the cache

Quick Start

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.

License

The MIT License.

About

swr improved version

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.2%
  • Other 0.8%