Skip to content

A react hook which can keep static state between functional components

License

Notifications You must be signed in to change notification settings

koab/react-use-static

Repository files navigation

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);
 ...
};

About

A react hook which can keep static state between functional components

Resources

License

Stars

Watchers

Forks

Packages