mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-04-26 18:48:28 -04:00
Update use-local-storage.ts
This commit is contained in:
parent
6f2018bd6e
commit
f684babb95
@ -1,6 +1,6 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
|
|
||||||
export const useLocalStorage = (key: string) => {
|
export const useLocalStorage = (key: string, allowNull = false) => {
|
||||||
const [value, setValue] = useState(() => {
|
const [value, setValue] = useState(() => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
let currentValue: any;
|
let currentValue: any;
|
||||||
@ -12,10 +12,11 @@ export const useLocalStorage = (key: string) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (value !== null) {
|
// don't write null values to storage unless specified
|
||||||
|
if (value !== null || allowNull) {
|
||||||
window.localStorage?.setItem(key, value);
|
window.localStorage?.setItem(key, value);
|
||||||
}
|
}
|
||||||
}, [key, value]);
|
}, [key, value, allowNull]);
|
||||||
|
|
||||||
return [value, setValue];
|
return [value, setValue];
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user