/
githubmirror
/
amphtml
Обзор
Документация
Войти
/
githubmirror
/
amphtml
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/preact/hooks/useLocalStorage.ts
38 строк
932 B
Scott Rippey
✨ [bento-list] Implemented bento-list component (#37360)
23 фев 2022, 21:06
Не верифицирован
23 фев 2022, 21:06
483540b
Код
Авторство
О чём код?
import {useCallback, useState} from '#preact'; import {logger} from '#preact/logger'; export function useLocalStorage<T>( key: string, defaultValue: T ): readonly [T, (newValue: T) => void] { // Keep track of the state locally: const [value, setValue] = useState(() => { try { const json = self.localStorage?.getItem(key); return json ? JSON.parse(json) : defaultValue; } catch (err) { // warning: could not read from local storage return defaultValue; } }); const storeValue = useCallback( (newValue: T) => { try { const json = JSON.stringify(newValue); self.localStorage?.setItem(key, json); } catch (err) { logger.warn( 'useLocalStorage', 'Could not write value to local storage', key, err ); } setValue(newValue); }, [key] ); return [value, storeValue] as const; }