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

use-local-storage-state

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-local-storage-state

React hook for using local storage the same way you use setState()

  • 0.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
106K
increased by0.56%
Maintainers
1
Weekly downloads
 
Created

What is use-local-storage-state?

The use-local-storage-state package is a React hook that simplifies the use of localStorage in React applications. It provides a way to persist state in localStorage, making it easy to manage and synchronize state with localStorage automatically.

What are use-local-storage-state's main functionalities?

Persisting State

This feature allows you to persist a piece of state in localStorage. The hook returns a stateful value and a function to update it, similar to useState, but the value is also stored in localStorage under the specified key.

import useLocalStorageState from 'use-local-storage-state';

function App() {
  const [name, setName] = useLocalStorageState('name', '');

  return (
    <div>
      <input
        type="text"
        value={name}
        onChange={(e) => setName(e.target.value)}
      />
      <p>Hello, {name}!</p>
    </div>
  );
}

Default Value

You can provide a default value that will be used if there is no existing value in localStorage. This makes it easy to initialize state with a default value.

import useLocalStorageState from 'use-local-storage-state';

function App() {
  const [count, setCount] = useLocalStorageState('count', 0);

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

JSON Support

The hook supports storing complex data structures like objects and arrays in localStorage by automatically handling JSON serialization and deserialization.

import useLocalStorageState from 'use-local-storage-state';

function App() {
  const [user, setUser] = useLocalStorageState('user', { name: '', age: 0 });

  return (
    <div>
      <input
        type="text"
        value={user.name}
        onChange={(e) => setUser({ ...user, name: e.target.value })}
      />
      <input
        type="number"
        value={user.age}
        onChange={(e) => setUser({ ...user, age: Number(e.target.value) })}
      />
      <p>{user.name} is {user.age} years old.</p>
    </div>
  );
}

Other packages similar to use-local-storage-state

Keywords

FAQs

Package last updated on 13 Mar 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc