1.0.7 • Published 5 years ago

@koab/react-use-static v1.0.7

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

react-use-static

A react hook which can keep static/shared state between functional components.

Live Demo

Install

npm install @koab/react-use-static or yarn add @koab/react-use-static

Example usage

Basically you need to assign an unique key to separate states between component types via second parameter. The third parameter is for persisting data even after each instance of component is unmounted.

import useStatic from 'react-use-static';

const Component = ({ defaultValue }) => {
 const persistDataEvenAfterUnmount = false;
 const uniqueComponentKey = 'ComponentKey';
 ...
 const [state, setValue] = useStatic(defaultValue, uniqueComponentKey, persistDataEvenAfterUnmount);
 ...
};