I keep having the error "React Hook useEffect has a missing dependency" I am trying to Save to Local in React and my app is working, but I cannot deploy it because of this warning. The warning is: **React Hook useEffect has a missing dependency: 'saveLocalTodos'. Either include it or remove the dependency array ** And my code is:
// Run once when the app starts
useEffect(() => {
getLocalTodos();
}, []);
// useEffect
useEffect(() => {
// Function
function filterHandler() {
switch (status) {
case `completed`:
setFilteredTodos(todos.filter((todo) => todo.completed === true));
break;
case `uncompleted`:
setFilteredTodos(todos.filter((todo) => todo.completed === false));
break;
default:
setFilteredTodos(todos);
break;
}
}
filterHandler();
saveLocalTodos();
}, [todos, status]);
// Save to Local
const saveLocalTodos = () => {
localStorage.setItem("todos", JSON.stringify(todos));
};
const getLocalTodos = () => {
if (localStorage.getItem("todos") === null) {
localStorage.setItem("todos", JSON.stringify([]));
} else {
let todoLocal = JSON.parse(localStorage.getItem(`todos`));
setTodos(todoLocal);
}
};
linter
, removing the[]
dependency will make the error go away, like this:javascript useEffect(() => getLocalTodos())
– sigfried