Home > Back-end >  Is there any alternative way to call a react hook conditionally?
Is there any alternative way to call a react hook conditionally?

Time:05-29

Using react hooks inside any function of condition is invalid. Neither can I do conditional rendering for a hook. Is there any way to use a hook with emplement condition? Please share code

CodePudding user response:

The only way

useEffect(() => {
  if (!!myparam) {
    console.log("myparam", myparam)
  }
}, [myparam])

CodePudding user response:

Can you please provide us any code snippet which your are trying to do?

If you are using useEffect hook then for conditional rendering you can do following code.

UseEffect(()=> { if(true) { Do some logic.}}, []);

UseEffect(()=> { Do some logic. }, [Add your state here])

  • Related