uselocalstroage
Version:
A React hook for interacting with localStorage.
53 lines (38 loc) • 1.34 kB
Markdown
A simple React hook for interacting with localStorage.
- **Easy Integration**: Quickly integrate local storage functionality into your React components.
- **Type-Safe Retrieval**: Use generics to ensure type safety when retrieving stored values.
- **Flexible Storage**: Store and retrieve both simple data types (strings, numbers) and complex objects.
- **Lightweight**: Keep your dependencies minimal with a small, standalone package.
## Installation
```bash
npm install uselocalstroage
```
## Quickstart
```jsx
import useLocalStorage from "uselocalstroage";
function App() {
// Initialize the useLocalStorage hook
const { getItem, setItem, delItem } = useLocalStorage();
// Example of using the hook in a React component
const handleSetItem = () => {
const data = { example: "value" };
setItem("exampleKey", data);
};
const handleGetItem = () => {
const data = getItem("exampleKey");
console.log("Retrieved data:", data);
};
const handleDeleteItem = () => {
delItem("exampleKey");
};
return (
<div>
<button onClick={handleSetItem}>Set Item</button>
<button onClick={handleGetItem}>Get Item</button>
<button onClick={handleDeleteItem}>Delete Item</button>
</div>
);
}
```