React Hook “useEffect” is called conditionally

后端 未结 4 2211
再見小時候
再見小時候 2020-12-15 04:53

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

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 05:05

    Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function. You can follow the documentation here.

    I couldn't find the use case in the above code. If you need the effect to run when the return value of firebase.getCurrentUsername() changes, you might want to use it outside the if condition like:

    useEffect(() => {
        firebase.getCurrentUserQuote().then(setQuote)
    }, [firebase.getCurrentUsername()]);
    

提交回复
热议问题