import { useState } from "react" ; export default function useLocalStorage ( key , initialValue ) { const [ storedValue , setStoredValue ] = useState ( ( ) => { if ( typeof window !== "undefined" ) { try { const item = localStorage . getItem ( key ) ; return item ? JSON . parse ( item ) : initialValue ; } catch ( error ) { console . log ( error ) ; return initialValue ; } } } ) ; const setValue = ( value ) => { try { const valueToStore = value instanceof Function ? value ( storedValue ) : value ; setStoredValue ( valueToStore ) ; localStorage . setItem ( key , JSON . stringify ( valueToStore ) ) ; } catch ( error ) { console . log ( error ) ; } } ; return [ storedValue , setValue ] ; }