PatternHookStoragePersistence
Local Storage Hook
A type-safe hook for persisting state to localStorage with automatic serialization and SSR compatibility.
Preview
Counter:0
Name:
Values persist in localStorage. Refresh the page to verify!
Code
import { useState, useEffect, useCallback } from 'react'
export function useLocalStorage<T>(
key: string,
initialValue: T
): [T, (value: T | ((prev: T) => T)) => void, () => void] {
// Initialize state with a function to avoid SSR issues
const [storedValue, setStoredValue] = useState<T>(() => {
if (typeof window === 'undefined') return initialValue
try {
const item = window.localStorage.getItem(key)
return item ? JSON.parse(item) : initialValue
} catch (error) {
console.warn(`Error reading localStorage key "${key}":`, error)
return initialValue
}
})
// Update localStorage when state changes
useEffect(() => {
if (typeof window === 'undefined') return
try {
window.localStorage.setItem(key, JSON.stringify(storedValue))
} catch (error) {
console.warn(`Error setting localStorage key "${key}":`, error)
}
}, [key, storedValue])
// Clear function
const clear = useCallback(() => {
setStoredValue(initialValue)
if (typeof window !== 'undefined') {
window.localStorage.removeItem(key)
}
}, [key, initialValue])
return [storedValue, setStoredValue, clear]
}
// Usage example:
function ThemeToggle() {
const [theme, setTheme] = useLocalStorage('theme', 'light')
return (
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Current: {theme}
</button>
)
}Looking for more?
Browse the full collection of patterns or check out other exploration topics.