2.1.37 • Published 8 months ago

@slimr/swr v2.1.37

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months 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

8 months ago

2.1.28

8 months ago

2.1.25

9 months ago

2.1.26

9 months ago

2.1.23

9 months ago

2.1.24

9 months ago

2.1.29

8 months ago

2.1.36

8 months ago

2.1.37

8 months ago

2.1.34

8 months ago

2.1.35

8 months ago

2.1.33

8 months ago

2.1.30

8 months ago

2.1.18

1 year ago

2.1.19

1 year ago

2.1.6

1 year ago

2.1.5

1 year ago

2.1.21

1 year ago

2.1.22

1 year ago

2.1.7

1 year ago

2.1.20

1 year ago

2.1.9

1 year ago

2.1.16

1 year ago

2.1.17

1 year ago

2.1.15

1 year ago

2.1.12

1 year ago

2.1.13

1 year ago

2.1.10

1 year ago

2.1.11

1 year ago

2.0.9

1 year ago

2.0.10

1 year ago

2.0.8

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago