useSessionStorage
Persist state in sessionStorage with automatic synchronization across tabs, type-safe storage and retrieval, and fallback value support.
Live Demo
Stored Value:
default value
Implementation
export function useSessionStorage<T>(key: string, initialValue: T) {
const [storedValue, setStoredValue] = useState<T>(() => {
if (typeof window === "undefined") return initialValue;
try {
const item = window.sessionStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.error(error);
return initialValue;
}
});
const setValue = (value: T | ((val: T) => T)) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
if (typeof window !== "undefined") {
window.sessionStorage.setItem(key, JSON.stringify(valueToStore));
}
} catch (error) {
console.error(error);
}
};
return [storedValue, setValue] as const;
}
Usage
const [value, setValue] = useSessionStorage("storage-key", initialValue);
// Use it like useState
setValue("new value");
// Or with a function
setValue(prev => prev + 1);
Important Notes
Server-Side Rendering (SSR) Limitations
This hook is designed to handle SSR environments safely. Since sessionStorage is only available in the browser:
- During SSR, the hook will return the initialValue
- sessionStorage operations are only performed on the client-side
- The hook includes checks for window object availability to prevent SSR errors
- Unlike localStorage, data persists only for the current session/tab