import { useState , useEffect } from 'react' ; export function useLocalStorageKey ( key ) { const [ localState , updateLocalState ] = useState ( ( ) => window . localStorage . getItem ( key ) ) ; useEffect ( ( ) => { const syncLocalStorage = ( event ) => { if ( event . key === key ) { updateLocalState ( event . newValue ) ; } } ; window . addEventListener ( 'storage' , syncLocalStorage ) ; return ( ) => { window . removeEventListener ( 'storage' , syncLocalStorage ) ; } ; } , [ key ] ) ; return localState ; }