-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(useLocalStorage): use own code
- Loading branch information
Showing
4 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
useCallback, | ||
useEffect, | ||
useState, | ||
Dispatch, | ||
SetStateAction, | ||
} from 'react'; | ||
|
||
export const useLocalStorage = <T>( | ||
key: string, | ||
initialValue: T, | ||
): [storedValue: T, setValue: Dispatch<SetStateAction<T>>] => { | ||
const readValue = useCallback(() => { | ||
try { | ||
const item = window.localStorage.getItem(key); | ||
return item ? (JSON.parse(item) as T) : initialValue; | ||
} catch (error) { | ||
if (typeof window === 'undefined') { | ||
console.warn(`Error reading localStorage key "${key}"`); | ||
} | ||
return initialValue; | ||
} | ||
}, [initialValue, key]); | ||
|
||
const [storedValue, setStoredValue] = useState(readValue); | ||
|
||
const saveToStorage = useCallback( | ||
(newValue: T) => { | ||
try { | ||
window.localStorage.setItem(key, JSON.stringify(newValue)); | ||
window.dispatchEvent(new Event('local-storage')); | ||
} catch (error) { | ||
if (typeof window === 'undefined') { | ||
console.warn(`Error setting localStorage key "${key}"`); | ||
} | ||
} | ||
}, | ||
[key], | ||
); | ||
|
||
const setValue = useCallback<Dispatch<SetStateAction<T>>>( | ||
(value) => { | ||
if (value instanceof Function) { | ||
setStoredValue((current) => { | ||
const newValue = value(current); | ||
saveToStorage(newValue); | ||
return newValue; | ||
}); | ||
} else { | ||
saveToStorage(value); | ||
setStoredValue(value); | ||
} | ||
}, | ||
[saveToStorage], | ||
); | ||
|
||
useEffect(() => { | ||
setStoredValue(readValue()); | ||
}, [readValue]); | ||
|
||
useEffect(() => { | ||
const handleStorageChange = () => { | ||
setStoredValue(readValue()); | ||
}; | ||
window.addEventListener('storage', handleStorageChange); | ||
window.addEventListener('local-storage', handleStorageChange); | ||
|
||
return () => { | ||
window.removeEventListener('storage', handleStorageChange); | ||
window.removeEventListener('local-storage', handleStorageChange); | ||
}; | ||
}, [readValue]); | ||
|
||
return [storedValue, setValue]; | ||
}; |