import { useState , useEffect } from 'react' ; export default function useStateWithStorage < T > ( localStorageKey , initial ) { const [ value , setValue ] = useState < T > ( ( ) => { const v = localStorage . getItem ( localStorageKey ) ; return v !== null ? JSON . parse ( v ) : initial ; } ) ; useEffect ( ( ) => { localStorage . setItem ( localStorageKey , JSON . stringify ( value ) ) ; } , [ value , localStorageKey ] ) ; return [ value , setValue ] ; }