diff --git a/hooks/use-local-storage.ts b/hooks/use-local-storage.ts index 818cc53a..837225af 100644 --- a/hooks/use-local-storage.ts +++ b/hooks/use-local-storage.ts @@ -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]; };