A React hook for caching data globally.
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.
# yarn
yarn add cache-hook
# npm
npm install --save cache-hook
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>
)
}
MIT © Tobias Herber