# @lindeneg/browser-cache

> Hook for caching data in localStorage.

Latest version **1.2.6** (published 2022-03-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lindeneg/browser-cache
pnpm add @lindeneg/browser-cache
yarn add @lindeneg/browser-cache
bun add @lindeneg/browser-cache
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.6 |
| Published | 2022-03-10 |
| First published | 2022-01-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | lindeneg |
| Maintainers | christianlindeneg |
| Keywords | react, hook, react-hooks, cache, typescript, use-cache, local-storage, use-local-storage |

## Links

- npm: https://www.npmjs.com/package/@lindeneg/browser-cache
- Repository: https://github.com/Lindeneg/lindeneg-npm-packages
- Homepage: https://github.com/lindeneg/lindeneg-npm-packages/tree/master/packages/browser-cache
- Issues: https://github.com/Lindeneg/lindeneg-npm-packages/issues
- npm.io page: https://npm.io/package/@lindeneg/browser-cache

## Dependencies (2)

- [@lindeneg/types](https://npm.io/package/@lindeneg/types.md) ^1.1.6
- [@lindeneg/ls-cache](https://npm.io/package/@lindeneg/ls-cache.md) ^2.0.7

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.2.6 (latest) — 2022-03-10
- 1.2.5 — 2022-02-19
- 1.2.4 — 2022-02-19
- 1.2.3 — 2022-02-13
- 1.2.2 — 2022-02-13
- 1.2.1 — 2022-02-07
- 1.2.0 — 2022-02-05
- 1.1.0 — 2022-01-30
- 1.0.8 — 2022-01-29
- 1.0.7 — 2022-01-23
- 1.0.6 — 2022-01-23
- 1.0.5 — 2022-01-20
- 1.0.4 — 2022-01-20
- 1.0.3 — 2022-01-16
- 1.0.2 — 2022-01-16
- … 2 more at https://npm.io/package/@lindeneg/browser-cache/versions

## README

### @lindeneg/browser-cache

![typescript](https://badgen.net/badge/icon/typescript?icon=typescript&label) ![bundle-size](https://badgen.net/bundlephobia/min/@lindeneg/browser-cache@1.2.3) ![license](https://badgen.net/npm/license/@lindeneg/browser-cache)

[Sandbox](https://codesandbox.io/s/lindeneg-browser-cache-q502j)

---

React hook for caching data in [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).

### Installation

`yarn add @lindeneg/browser-cache`

### Usage

```tsx
import useBrowserCache from '@lindeneg/browser-cache';

type SomeCacheType = {
  ...
}

function SomeComponent() {
  const { cache } = useBrowserCache<SomeCacheType>(config);

  // set item
  cache.set('id', 1);

  // get item
  cache.value('id');

  // listen to event
  cache.on('trim', (removed) => {
    console.log('trim removed these keys from cache: ', removed);
  });

  // and so on
}
```

Or with `React.Context` for a shared cache to be used by multiple components.

```tsx
import {
  BrowserCacheContextProvider,
  useCacheContext,
} from '@lindeneg/browser-cache';

function ProviderComponent({ children }: { children: React.ReactNode }) {
  return (
    <BrowserCacheContextProvider config={config}>
      {children}
    </BrowserCacheContextProvider>
  );
}

function ConsumerComponent() {
  const { cache } = useCacheContext<SomeCacheType>();

  // set item
  cache.set('id', 1);

  // and so on

  return <div></div>;
}
```

The documentation [here](https://github.com/Lindeneg/lindeneg-npm-packages/tree/master/packages/ls-cache) can be used.

---
_Source: https://npm.io/package/@lindeneg/browser-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
