Type of the stored value
localStorage key to use for storage
Initial value if key doesn't exist in localStorage
Tuple of [storedValue, setValue] similar to useState
// Store user's preferred theme
const [theme, setTheme] = useLocalStorage('theme', 'light');
return (
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Toggle Theme
</button>
);
Custom hook to sync state with localStorage
Business Perspective
Enables widgets to remember user preferences and settings across browser sessions, improving user experience by maintaining personalized configurations. This is particularly valuable for security officers who configure specific security levels and want their settings persisted. 💾
Technical Perspective
Provides a React state hook that automatically syncs with localStorage, handling JSON serialization/deserialization and SSR compatibility. Uses the same API as useState for familiarity.