Created
October 2, 2023 17:05
-
-
Save emekaorji/cb1c86fcd626320bebb7362f216fcef3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect, Dispatch, SetStateAction } from 'react'; | |
function useLocalStorage<T>( | |
_key: string, | |
_initialValue: T | (() => T) | |
): [T, Dispatch<SetStateAction<T>>]; | |
function useLocalStorage<T = undefined>( | |
_key: string | |
): [T | undefined, Dispatch<SetStateAction<T | undefined>>]; | |
function useLocalStorage<T = undefined>( | |
key: string, | |
initialValue?: T | (() => T) | |
) { | |
const [value, setValue] = useState(() => { | |
if (typeof window !== 'undefined') { | |
const storedValue = localStorage.getItem(key); | |
return storedValue ? (JSON.parse(storedValue) as T) : initialValue; | |
} else { | |
return initialValue; | |
} | |
}); | |
useEffect(() => { | |
localStorage.setItem(key, JSON.stringify(value)); | |
}, [key, value]); | |
useEffect(() => { | |
const handleStorageChange = (event: StorageEvent) => { | |
if (event.key === key) | |
setValue(event.newValue ? JSON.parse(event.newValue) : initialValue); | |
}; | |
window.addEventListener('storage', handleStorageChange); | |
return () => { | |
window.removeEventListener('storage', handleStorageChange); | |
}; | |
}, [key, initialValue]); | |
return [value, setValue] as const; | |
} | |
export default useLocalStorage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment