UNPKG

sanity

Version:

Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches

14 lines (10 loc) 426 B
import {type Dispatch, type SetStateAction, useEffect, useState} from 'react' export function useLocalState<T>(key: string, defaultValue: T): [T, Dispatch<SetStateAction<T>>] { const [value, setValue] = useState<T>(() => JSON.parse(localStorage.getItem(key) ?? JSON.stringify(defaultValue)), ) useEffect(() => { localStorage.setItem(key, JSON.stringify(value)) }, [key, value]) return [value, setValue] }