Written by
react-style
on
on
useLocalStorage
useLocalStorage
import {useState} from "react"; const useLocalStorage =(key:string='',initialValue:string='')=>{ const [state,setState] =useState(()=>{ try{ const item = window.localStorage.getItem(key); return item ? JSON.parse(item) : initialValue; }catch (error) { return initialValue; } }); const setLocalStorageState = (newState:any)=>{ try{ const newStateValue = typeof newState ==='function' ? newState(state) : newState; setState(newStateValue); window.localStorage.setItem(key, JSON.stringify(newStateValue)); }catch(error) { console.error(`Unable to store new value for ${key} in localStorage`); } } } export default useLocalStorage;
공유하기 글 요소 저작자표시
from http://taesic.tistory.com/30 by ccl(A) rewrite - 2022-01-01 12:01:05