use-storage-hooks is a lightweight, easy-to-use library that provides React hooks for working with browser storage (localStorage and sessionStorage). It simplifies state management by enabling seamless integration of persistent storage into your React components.
This library provides a hassle-free solution for managing state persistence across sessions. Whether you need to store user preferences or temporary data, use-storage-hooks helps you integrate browser storage without repetitive boilerplate code.
To install the library, use npm or yarn:
npm install use-storage-hooksor
yarn add use-storage-hooksThis hook allows you to manage state that is synchronized with localStorage.
import { useLocalStorage } from 'use-storage-hooks';
const [value, setValue] = useLocalStorage<number>('key');key(string): The key under which the value is stored inlocalStorage.
value(T | undefined): The current value associated with the key. It returns the value of the generic typeTorundefined.setValue(function): A function to update the value. It accepts a value of typeTorundefined. Ifundefinedis passed, the value will be removed fromlocalStorage.
This hook allows you to manage state that is synchronized with sessionStorage.
import { useSessionStorage } from 'use-storage-hooks';
const [value, setValue] = useSessionStorage<number>('key');key(string): The key under which the value is stored insessionStorage.
value(T | undefined): The current value associated with the key. It returns the value of the generic typeTorundefined.setValue(function): A function to update the value. It accepts a value of typeTorundefined. Ifundefinedis passed, the value will be removed fromsessionStorage.
import React from 'react';
import { useLocalStorage, useSessionStorage } from 'use-storage-hooks';
function ComponentA() {
const [localValue, setLocalValue] = useLocalStorage<number>('local-key');
const [sessionValue, setSessionValue] = useSessionStorage<number>('session-key');
return (
<div>
<p>Local Storage Value: {localValue}</p>
<button onClick={() => setLocalValue(Math.random())}>Generate Random Local Number</button>
<button onClick={() => setLocalValue(undefined)}>Remove Local Value</button>
<p>Session Storage Value: {sessionValue}</p>
<button onClick={() => setSessionValue(Math.random())}>Generate Random Session Number</button>
<button onClick={() => setSessionValue(undefined)}>Remove Session Value</button>
</div>
);
}
function ComponentB() {
const [localValue] = useLocalStorage<number>('local-key');
const [sessionValue] = useSessionStorage<number>('session-key');
return (
<div>
<p>Local Storage Value: {localValue}</p>
<p>Session Storage Value: {sessionValue}</p>
</div>
);
}
function App() {
return (
<div>
<ComponentA />
<ComponentB />
</div>
);
}
export default App;In this example, ComponentA and ComponentB share the same keys for both localStorage ('local-key') and sessionStorage ('session-key'). When the values are updated in ComponentA, they are immediately reflected in ComponentB. The "Generate Random Local Number" and "Generate Random Session Number" buttons set new random numbers, and the "Remove" buttons remove the values from localStorage and sessionStorage respectively.
Both hooks use generics to allow you to specify the type of the stored value. By default, the type is unknown, but you can specify a different type when using the hooks.
import { useLocalStorage } from 'use-storage-hooks';
const [value, setValue] = useLocalStorage<number>('key');In this example, value will be of type number and setValue will accept a value of type number or undefined.
MIT