2.1.37 • Published 2 years ago

@slimr/swr v2.1.37

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

🪶 @slimr/swr npm package

A tiny (~600B when bundled) hook that accepts a function callback, calls the function and returns a reactive callback state. Uses a cache and will return the cache value if available while waiting for the callback to complete, then update the return on complete. This is often called 'stale-while-refresh' and abbreviated as 'SWR', hence the name of the hook. Source is in @slimr/swr.

  • Only 440 bytes (minified + gzipped)
  • Shows cached data immediately and updates the UI when the callback resolves
  • Deduplicates concurrent requests: runs the callback only once if duplicates are requested
  • UX: no flickering, no waiting if cached, enables native scroll restoration

Context

@slimr is a set of slim React (hence '@slimr') libs. Check them all out on github!

Options

  • throttle - Throttle threshold in ms: time that the cache is deemed current, to avoid over re-fetching

Usage

import {useSWR} from `@slimr/swr`

function MyComponent({ page }: number) {
  const { result, loading, refresh} = useSWR(() => getPageData(page), [page], {throttle: Infinity})
  if (loading) return null
  return (
    <section>
      <h1>{result.title}</h1>
      <p>{result.description}</h1>
      <button onClick={refresh}>Refresh</button>
    </section>
  )
}
2.1.27

2 years ago

2.1.28

2 years ago

2.1.25

2 years ago

2.1.26

2 years ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.29

2 years ago

2.1.36

2 years ago

2.1.37

2 years ago

2.1.34

2 years ago

2.1.35

2 years ago

2.1.33

2 years ago

2.1.30

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.21

2 years ago

2.1.22

2 years ago

2.1.7

2 years ago

2.1.20

2 years ago

2.1.9

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.15

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.0.9

2 years ago

2.0.10

2 years ago

2.0.8

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago