# preact-swr-store

> Preact bindings for swr-store

Latest version **0.10.6** (published 2023-02-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install preact-swr-store
pnpm add preact-swr-store
yarn add preact-swr-store
bun add preact-swr-store
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.10.6 |
| Published | 2023-02-21 |
| First published | 2021-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 2 |
| Unpacked size | 22.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Alexis Munsayac |
| Maintainers | lxsmnsyc |
| Keywords | pridepack |

## Links

- npm: https://www.npmjs.com/package/preact-swr-store
- Repository: https://github.com/lxsmnsyc/swr-store
- Homepage: https://github.com/lxsmnsyc/swr-store/tree/main/packages/preact-swr-store
- Issues: https://github.com/lxsmnsyc/swr-store/issues
- npm.io page: https://npm.io/package/preact-swr-store

## Dependencies (2)

- [@lyonph/preact-hooks](https://npm.io/package/@lyonph/preact-hooks.md) ^0.5.1
- [preact-store-adapter](https://npm.io/package/preact-store-adapter.md) ^0.4.4

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.10.6 (latest) — 2023-02-21
- 0.10.5 — 2023-02-13
- 0.10.4 — 2022-07-28
- 0.10.3 — 2022-07-20
- 0.10.2 — 2022-03-05
- 0.10.0 — 2022-01-18
- 0.9.4 — 2021-06-15
- 0.9.3 — 2021-06-14
- 0.9.2 — 2021-05-06
- 0.9.1 — 2021-04-26
- 0.9.0 — 2021-04-23
- 0.8.2 — 2021-04-17
- 0.8.1 — 2021-04-17
- 0.8.0 — 2021-04-17
- 0.7.1 — 2021-04-05
- … 1 more at https://npm.io/package/preact-swr-store/versions

## README

# preact-swr-store

> Preact bindings for `swr-store`

[![NPM](https://img.shields.io/npm/v/preact-swr-store.svg)](https://www.npmjs.com/package/preact-swr-store) [![JavaScript Style Guide](https://badgen.net/badge/code%20style/airbnb/ff5a5f?icon=airbnb)](https://github.com/airbnb/javascript)[![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?style=flat-square&logo=codesandbox)](https://codesandbox.io/s/github/LXSMNSYC/swr-store/tree/main/examples/preact-swr-store)

## Install

```bash
npm install --save swr-store preact-swr-store
```

```bash
yarn add swr-store preact-swr-store
```

## Usage

```tsx
import { Suspense } from 'preact';
import { createSWRStore } from 'swr-store';
import { useSWRStore, SWRStoreRoot } from 'preact-swr-store';

const API = 'https://dog.ceo/api/breed/';
const API_SUFFIX = '/images/random';

interface APIResult {
  message: string;
  status: string;
}

const dogAPI = createSWRStore<APIResult, [string]>({
  key: (breed: string) => breed,
  get: async (breed: string) => {
    const response = await fetch(`${API}${breed}${API_SUFFIX}`);
    return (await response.json()) as APIResult;
  },
  revalidateOnFocus: true,
  revalidateOnNetwork: true,
});

function DogImage(): JSX.Element {
  const data = useSWRStore(dogAPI, ['shiba'], {
    suspense: true,
  });

  return <img src={data.message} alt={data.message} />;
}

function Trigger(): JSX.Element {
  return (
    <button
      type="button"
      onClick={() => {
        dogAPI.trigger(['shiba']);
      }}
    >
      Trigger
    </button>
  );
}

export default function App(): JSX.Element {
  return (
    <SWRStoreRoot>
      <Trigger />
      <div>
        <Suspense fallback={<h1>Loading...</h1>}>
          <DogImage />
        </Suspense>
      </div>
    </SWRStoreRoot>
  );
}
```

## API

### `<SWRStoreRoot>`

Must be located at the root of the app/tree.

### `useSWRStore(store, args, options)`

Subscribes to an SWR store, passing `args`, which are received by the corresponding store for data-fetching and cache updates.

`options` has the following properties:

- `suspense`: When `true`, suspends the component when receiving the result, if the result status is `'pending'` until the result resolves,where `useSWRStore` returns the resolved data. Otherwise, `useSWRStore` returns the result. Defaults to `false`.
- `initialData`: Allows lazy hydration when reading the store. If the store does not have cache, `initialData` hydrates the cache and attempts a revalidation. If no `initialData` is provided, defaults to store's `options.initialData`.
- `shouldRevalidate`: If `true`, goes through the revalidation process when reading through the cache. Defaults to `true`.

## License

MIT © [lxsmnsyc](https://github.com/lxsmnsyc)

---
_Source: https://npm.io/package/preact-swr-store · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
