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 value | Type | Description | Defualt |
---|---|---|---|
key | string | Key of the localStorage object | undefined |
defaultValue | any | Default initial value | null |
Returnsβ
Returns an array of following items:
Return value | Type | Description |
---|---|---|
value | any | value stored in localStorage |
set | Function | set value stored in localStorage |
remove | Function | remove 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.