Skip to content

ncheungg/use-sticky-state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published