1.2.6 • Published 2 years ago

@lindeneg/browser-cache v1.2.6

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

@lindeneg/browser-cache

typescript bundle-size license

Sandbox


React hook for caching data in localStorage.

Installation

yarn add @lindeneg/browser-cache

Usage

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.

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 can be used.

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago