New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dteel/usestoredstate

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dteel/usestoredstate

useStoredState, kind of like useState except it will persist in localStorage/sessionStorage/equivelant object. Will stay in sync with other hooks across your app and across brower windows.

0.1.2
latest
Source
npm
Version published
Maintainers
1
Created
Source

useStoredState

useStoredState is a custom react hook that can persist using either localStorage, sessionStorage, or an object that follows the same rules.

Example Usage

In the following code, both components will stay in sync with each other, and if you open up two browser windows (they have to share localStorage so keep them the same, eg chrome/chrome). If you do a page refresh it will load the previous state and persists.

If you create an object that has setItem/getItem/removeItem function propertys that follow the same rules as localStorage, you can create your own adapter essentially to save state to a server or whatever else you want to do.

import './App.css';
import { useStoredState } from './useStoredState';

const Name = ({nameId}) => {
  const [name, setName] = useStoredState(localStorage, 'stored', nameId, 'Dan Teel');
  return <input type='text' value={name ? name.name : ''} onChange={(e)=>setName({name: e.target.value})}/>
}

function App() {
  return (
    <div className='App'>
      <Name nameId='name'/>
      <Name nameId='name'/>
    </div>
  );
}

export default App;

FAQs

Package last updated on 12 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts