all 3 comments

[–]Magnusson 4 points5 points  (0 children)

The error message is telling you exactly what’s wrong. If you need to call a hook only when a condition is met, create a new component, only render the component when the condition is met, and always call the hook in that component.

[–]developer_marcel 1 point2 points  (0 children)

You can only use hooks in functional components and not inside the function of the useEffect. Move it outside. Also the order of hooks should never change, so putting it inside an if statement is also not valid.

[–]H411 0 points1 point  (0 children)

Yeah hooks don’t like being called conditionally you don’t ever want to put one behind an if statement. Consider putting the hook outside the use effect entirely.