github.com/thanos-io/thanos@v0.32.5/pkg/ui/react-app/src/hooks/useLocalStorage.tsx (about) 1 import { Dispatch, SetStateAction, useEffect, useState } from 'react'; 2 3 export function useLocalStorage<S>(localStorageKey: string, initialState: S): [S, Dispatch<SetStateAction<S>>] { 4 const localStorageState = JSON.parse(localStorage.getItem(localStorageKey) || JSON.stringify(initialState)); 5 const [value, setValue] = useState(localStorageState); 6 7 useEffect(() => { 8 const serializedState = JSON.stringify(value); 9 localStorage.setItem(localStorageKey, serializedState); 10 }, [localStorageKey, value]); 11 12 return [value, setValue]; 13 }