plane/apps/app/hooks/use-local-storage.tsx

23 lines
580 B
TypeScript
Raw Normal View History

import React, { useEffect, useState } from "react";
const getSavedValue = (key: any, value: any) => {
const savedValue = localStorage.getItem(key);
if (savedValue) {
return savedValue;
}
return value;
};
const useLocalStorage = (key: any, value: any) => {
const [updatedvalue, seUpdatedvalue] = useState(() => getSavedValue(key, value));
useEffect(() => {
localStorage.setItem(key, updatedvalue);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [updatedvalue]);
return [updatedvalue, seUpdatedvalue];
};
export default useLocalStorage;