Skip to content

Instantly share code, notes, and snippets.

@emekaorji
Created October 2, 2023 17:05
Show Gist options
  • Save emekaorji/cb1c86fcd626320bebb7362f216fcef3 to your computer and use it in GitHub Desktop.
Save emekaorji/cb1c86fcd626320bebb7362f216fcef3 to your computer and use it in GitHub Desktop.
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