Spaces:
Configuration error
Configuration error
| // FIX: Import `React` to use for type annotations like `React.Dispatch`. | |
| import React, { useState, useEffect } from 'react'; | |
| export function useLocalStorage<T,>(key: string, initialValue: T): [T, React.Dispatch<React.SetStateAction<T>>] { | |
| const [storedValue, setStoredValue] = useState<T>(() => { | |
| try { | |
| const item = window.localStorage.getItem(key); | |
| return item ? JSON.parse(item) : initialValue; | |
| } catch (error) { | |
| console.error(error); | |
| return initialValue; | |
| } | |
| }); | |
| useEffect(() => { | |
| try { | |
| const valueToStore = | |
| typeof storedValue === 'function' | |
| ? storedValue(storedValue) | |
| : storedValue; | |
| window.localStorage.setItem(key, JSON.stringify(valueToStore)); | |
| } catch (error) { | |
| console.error(error); | |
| } | |
| }, [key, storedValue]); | |
| return [storedValue, setStoredValue]; | |
| } | |