#react-hooks/exhaustive-deps

7 messages · Page 1 of 1 (latest)

late ingot
#

I have this error message :
55:6 Warning: React Hook useEffect has a missing dependency: 'index'. Either include it or
remove the dependency array. You can also replace multiple useState variables with useReducer if 'setAnswersHistoric' needs the current value of 'index'. react-hooks/exhaustive-deps

But the problem is that if I put index in the dependencies it will break down the logic.

night axleBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

late ingot
radiant prism
# late ingot

seems like you can just get rid of the entire newAnswer state

const handleContinueAction = (answer: string) => {
  if (answer) {
    setIndex((prevIndex) => Math.min(prevIndex + 1, exercices.length - 1));
    setAnswers((prevAnswers) => [...prevAnswers, answer]);
    setAnswersHistoric((prevAnswers) => {
      const updatedAnswers = [...prevAnswers]; // Crée une copie du tableau
      updatedAnswers[index - 1] = answer; // Met à jour l'élément à l'index spécifié
      return updatedAnswers; // Retourne le nouveau tableau mis à jour
    });
  }
};
#

see more: https://react.dev/learn/you-might-not-need-an-effect
in this case you are listening to changes in newAnswer to update some states based on the new newAnswer value. but... you could just remove the setNewAnswer and update the states directly in the event handler

late ingot
#

Ok Thanks

#

Yes it works thanks