mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
30 lines
673 B
JavaScript
30 lines
673 B
JavaScript
import { useState } from "react";
|
|
import toastError from "../../errors/toastError";
|
|
|
|
export function useLocalStorage(key, initialValue) {
|
|
const [storedValue, setStoredValue] = useState(() => {
|
|
try {
|
|
const item = localStorage.getItem(key);
|
|
return item ? JSON.parse(item) : initialValue;
|
|
} catch (error) {
|
|
toastError(error);
|
|
return initialValue;
|
|
}
|
|
});
|
|
|
|
const setValue = value => {
|
|
try {
|
|
const valueToStore =
|
|
value instanceof Function ? value(storedValue) : value;
|
|
|
|
setStoredValue(valueToStore);
|
|
|
|
localStorage.setItem(key, JSON.stringify(valueToStore));
|
|
} catch (error) {
|
|
toastError(error);
|
|
}
|
|
};
|
|
|
|
return [storedValue, setValue];
|
|
}
|