UNPKG

react-reuse-hooks

Version:

A collection of 30+ production-ready reusable React hooks for web apps, covering state, effects, media, forms, and utilities.

20 lines (16 loc) 496 B
import { useState } from "react"; export function useSessionStorage(key, initialValue) { const [storedValue, setStoredValue] = useState(() => { try { const item = sessionStorage.getItem(key); return item ? JSON.parse(item) : initialValue; } catch (error) { return initialValue; } }); const setValue = (value) => { setStoredValue(value); sessionStorage.setItem(key, JSON.stringify(value)); }; return [storedValue, setValue]; }