cache-hook

A React hook for caching data globally.

Usage no npm install needed!

<script type="module">
  import cacheHook from 'https://cdn.skypack.dev/cache-hook';
</script>

README

Use Cache

A React hook for caching data globally.

About

React's useMemo is great, but it only caches the result for the lifetime of a single component. This hook ensures that an expensive operation will only be performed once for the same input within the lifetime of a whole page load.

Install

# yarn
yarn add cache-hook

# npm
npm install --save cache-hook

Usage

import createCache from 'cache-hook';

let useCache = createCache(data => {
  // perform expensive operation
  return result;
})

let Component = () => {
  let cached = useCache('input');

  return (
    <div>
      Result: {cached}
    </div>
  )
}

License

MIT © Tobias Herber

Made by Varld