use-local-storage-hook-react
v1.0.1
Published
A simple React hook to manage local storage with ease.
Downloads
4
Readme
use-local-storage-hook
A simple React hook to manage local storage with ease.
Installation
To install the package, run:
npm install use-local-storage-hook
Usage
To use the useLocalStorage
hook, import it into your component and use it as shown below:
import React from 'react';
import { useLocalStorage } from 'use-local-storage-hook';
const App: React.FC = () => {
const [name, setName] = useLocalStorage<string>('name', 'John Doe');
return (
<div>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<p>{name}</p>
</div>
);
};
export default App;
API
useLocalStorage<T>(key: string, initialValue: T)
A hook to manage local storage.
Parameters
key
(string): The key to store the value under in local storage.initialValue
(T): The initial value to set if there is no value in local storage.
Returns
[storedValue, setValue]
(Array): Returns the current value and a function to update it.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.