Skip to main content
Version: Next

useLocalstorageState

About​

UseState but auto updates values to localStorage

Installation​

npm install --save rooks

Importing the hook​

import { useLocalstorageState } from "rooks";

Usage​

function Demo() {
const [count, setCount] = useLocalstorageState("my-app:count", 0);

return (
<div className="App">
<h1>Rooks : useLocalstorageState</h1>
<button onClick={() => setCount(0)}>clear</button>
<button onClick={() => setCount(count + 1)}>{count}</button>
</div>
);
}

render(<Demo />);

Arguments​

Argument valueTypeDescriptionDefualt
keystringKey of the localStorage objectundefined
defaultValueanyDefault initial valuenull

Returns​

Returns an array of following items:

Return valueTypeDescription
valueanyvalue stored in localStorage
setFunctionset value stored in localStorage
removeFunctionremove value stored in localStorage

Codesandbox Examples​

Basic Usage​

Join Bhargav's discord server​

You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.