mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Signed-off-by: Smirnov Aleksey <aleksey.smirnov@sbermarket.ru> Signed-off-by: Smirnov Aleksey <aleksey.smirnov@sbermarket.ru> Co-authored-by: Smirnov Aleksey <aleksey.smirnov@sbermarket.ru>
15 lines
514 B
TypeScript
15 lines
514 B
TypeScript
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
|
|
|
|
export function useLocalStorage<S>(localStorageKey: string, initialState: S): [S, Dispatch<SetStateAction<S>>] {
|
|
const [value, setValue] = useState(() =>
|
|
JSON.parse(localStorage.getItem(localStorageKey) || JSON.stringify(initialState))
|
|
);
|
|
|
|
useEffect(() => {
|
|
const serializedState = JSON.stringify(value);
|
|
localStorage.setItem(localStorageKey, serializedState);
|
|
}, [localStorageKey, value]);
|
|
|
|
return [value, setValue];
|
|
}
|