Skip to content
Advertisement

React Hook “useEffect” is called conditionally

React is complaining about code below, saying it useEffect is being called conditionally:

JavaScript

And that returns me the error:

React Hook “useEffect” is called conditionally. React Hooks must be called in the exact same order in every component render.

Does anyone happen to know what the problem here is?

Advertisement

Answer

Your code, after an if statement that contains return, is equivalent to an else branch:

JavaScript

Which means that it’s executed conditionally (only when the return is NOT executed).

To fix:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement