@reactutils/use-local-storage
Version:
A custom hook that provides a simple interface to the browser's local storage.
41 lines (30 loc) • 1.26 kB
Markdown
# /use-local-storage
[][package-npm-link]
[][package-bundlephobia]
> A custom hook that provides a simple interface to the browser's local storage.
Store a key-value pair in the browser's local storage. The value will persist even after the browser is closed. Since the local storage API isn't available in server-rendering environments, we check that `typeof window !== "undefined"` to make SSR and SSG work properly.
## Installation
```sh
npm install /use-local-storage
# or
yarn add /use-local-storage
```
## Usage
```jsx
function App() {
// Similar to useState but first arg is key, and the second is the default value.
const [name, setName] = useLocalStorage<string>("name", "Bob");
return (
<div>
<input
type="text"
placeholder="Enter your name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
);
}
```
[package-npm-link]: https://www.npmjs.com/package/@reactutils/use-local-storage
[package-bundlephobia]: https://bundlephobia.com/package/@reactutils/use-local-storage