1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-26 12:38:27 -04:00

Update use-local-storage.ts

This commit is contained in:
Jake Jarvis 2022-04-10 10:43:39 -04:00
parent 6f2018bd6e
commit f684babb95
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39

View File

@ -1,6 +1,6 @@
import { useState, useEffect } from "react";
export const useLocalStorage = (key: string) => {
export const useLocalStorage = (key: string, allowNull = false) => {
const [value, setValue] = useState(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let currentValue: any;
@ -12,10 +12,11 @@ export const useLocalStorage = (key: string) => {
});
useEffect(() => {
if (value !== null) {
// don't write null values to storage unless specified
if (value !== null || allowNull) {
window.localStorage?.setItem(key, value);
}
}, [key, value]);
}, [key, value, allowNull]);
return [value, setValue];
};