Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

use-sticky-state

ncheungg44ISC1.0.3

Custom hook to persist React state

react, localstorage state, local storage react, storage, useState, persist state, persist storage, persistant storage, persistant state, useStickyState

readme

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);
Property Description
defaultValue The default value used if storageKey was not previously set
storageKey Unique string key used to persist data, using your browser's built in localStorage API