1.0.3 • Published 2 years ago

use-sticky-state v1.0.3

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

use-sticky-state

Custom hook to persist React state on browser

If you're looking to persist React Native state, check out react-native-sticky-state

⚙️ Installation

In your React project folder, run:

npm i use-sticky-state --save

💻 Example

import useStickyState from "use-sticky-state";

const App = () => {
  const [count, setCount] = useStickyState(0, "counter-key");

  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>

      <p>{count}</p>
    </div>
  );
};

📚 Usage

const [state, setState] = useStickyState(defaultValue, storageKey);
PropertyDescription
defaultValueThe default value used if storageKey was not previously set
storageKeyUnique string key used to persist data, using your browser's built in localStorage API