Accepted answer

From react documentation you can't use a hook with conditionnal operator.

You can call hook all the time and store the result in a variable, and just condition the render instead.

const input = useInput(input, inputName);
{this.state.showMyInput && input}


my problem was that I had used the useState hooks after a return statement on my code, like:

function myComponenet () {
   if(loading) {return <Loading/>}

   const [myVar, setMyVar] = React.useState({})

   // my main component UI begins here


I had to move this const [myVar, setMyVar] = React.useState({}) line, above the if statement and that solved my problem


As per the react docs, you shouldn't invoke hooks inside a conditional statement. Instead, use the conditional inside your hook: As explained here

Related Query

More Query from same tag