I've seen similar issues on this site, but I didn't understood properly why it occurs.
What I'm facing, is that I'm trying to make a Todo List with React. It's my first time using and building a project with react, so I'm still learning.
So, what I want to do, is when a user clicks on a button, a modal opens, then it collects the title and the details of that todo, the user clicks save and the modal closes, then a card is displayed displaying that todo.
Code:
let firstRender = useRef(true);
useEffect(()=> {
if(firstRender.current) {
firstRender.current = false;
}
else{
if(!modalState) {
if(!modalCancel) {
let Title = todoTitle;
let Detail = todoDetail;
setTodo([...todo, {
title: Title.toUpperCase(),
detail: Detail
}])
}
}
}
}, [modalState, modalCancel])
The modalState is just a state that tells if the modal is open or closed and the modalCancel is a state which tells if the modal is saved or cancelled. I have some other states as well which are responsible for getting the details and title of the todo from the modal, and a state which keeps track of all the todos.
This works fine but displays the following warning in the terminal :
React Hook useEffect has missing dependencies: 'todo', 'todoDetail', and 'todoTitle'. Either include them or remove the dependency array. You can also do a functional update 'setTodo(t => ...)' if you only need 'todo' in the 'setTodo' call react-hooks/exhaustive-deps
If I add all of the states, my app does not work, and I don't want to remove the two dependencies because I want to fire re-render everytime (except the first when the page loads) when the modal closes. Can anyone help me with that? I can show how the all states are written if needed.
todo,todoDetailandtodoTitle?