-1

I'm using the moviedb api to make an app and i'm having some trouble updating state inside a ternary operator. If i run the following code to check if everything is working there are no problems

{(match.params.id === iteminfo[0]) ? console.log("match") : console.log("nomatch")}

If the id and iteminfo match , i get match , if they don't i get "nomatch"

when i try and update the state like this

{(match.params.id === iteminfo[0]) ? setRemove(true) : console.log("nomatch")}

I get the following error

enter image description here

Is there a way to update the state so it doesn't form this loop?

  • Does this answer your question? [Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop](https://stackoverflow.com/questions/55265604/uncaught-invariant-violation-too-many-re-renders-react-limits-the-number-of-re) – Emile Bergeron Aug 18 '20 at 03:20

1 Answers1

2

Problem is that you must’ve put this ternary inside the render function, which produces a feedback loop. Wrap it inside a useEffect callback will do.

hackape
  • 18,643
  • 2
  • 29
  • 57