A React hook for managing localStorage with expiry
npm install react-localstorage-hook-with-expiryA simple yet powerful React hook for managing localStorage with expiration support
- 🕒 Automatic data expiration
- 🔄 Seamless sync between localStorage and component state
- 🛡️ SSR compatible (Next.js ready)
- 💪 TypeScript support out of the box
- 🚀 Zero dependencies
``bash`
npm install react-localstorage-hook-with-expiry
`jsx
import useLocalStorage from "react-localstorage-hook-with-expiry";
function MyComponent() {
const [token, setToken] = useLocalStorage("auth_token", null, {
expiry: 24 60 60 * 1000, // 24 hours
});
// ...
}
`
| Parameter | Type | Default | Description |
| ------------ | ------ | ------- | ---------------------- |
| key | string | - | localStorage key |
| initialValue | any | - | Initial value |
| options | object | {} | Optional configuration |
| Option | Type | Default | Description |
| ------- | ------- | ------- | ------------------------------- |
| expiry | number | null | Expiration time in milliseconds |
| encrypt | boolean | false | Enable data encryption |
`tsx`
const [user, setUser] = useLocalStorage
expiry: 3600000, // 1 hour
});
`jsx``
const [data, setData, removeData] = useLocalStorage("temp_data");
// ...
removeData(); // Remove from localStorage
Suggestions and PRs are always welcome!
MIT © [AmirAghajani98]