uselocalstroage
v3.1.10
Published
A React hook for interacting with localStorage.
Downloads
18
Maintainers
Readme
uselocalstroage
A simple React hook for interacting with localStorage.
Features
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
npm install uselocalstroage
Quickstart
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>
);
}