Playing around with localStorage in React by making a very simple to-do app. I've found examples of localStorage hooks like this one:
function getStorageValue(key, defaultValue) {
// getting stored value
const saved = localStorage.getItem(key);
const initial = JSON.parse(saved);
return initial || defaultValue;
}
export const useLocalStorage = (key, defaultValue) => {
const [value, setValue] = useState(() => {
return getStorageValue(key, defaultValue);
});
useEffect(() => {
// storing input name
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
return [value, setValue];
};
I understand this but I'm trying to figure out how to set the defaultvalue to be a key value fetched and parsed from a local file.
Like this:
const [name, setName] = useLocalStorage("name", JSON file data);