Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement in React Hooks

前端 未结 4 1576
暖寄归人
暖寄归人 2021-01-07 18:06

Given the following component, when I press down on the age selector and change the value to 15, such that I render a form without the driver license field, I get the error:

4条回答
  •  情书的邮戳
    2021-01-07 18:31

    Make sure that you didn't run useEffect conditionally.

    For example, if you have some code like the following:

    if(condition) {
      useEffect(()=>{
        doSomething();
      }, []);
    }
    

    Then change it to

    useEffect(()=>{
      if(condition) {
        doSomething();
      }
    }, []);
    

    Then the error would not happen.

提交回复
热议问题