React Hook “useState” is called in function “app” which is neither a React function component or a custom React Hook function

前端 未结 29 1068
花落未央
花落未央 2020-11-30 00:07

I\'m trying to use react hooks for a simple problem

const [personState,setPersonState] = useState({ DefinedObject });

with following depend

29条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 00:38

    First of all, you need to uppercase the FirstLetter of your components, in your case app should be App and person should be Person.

    I tried to copy your code in the hope of finding the issue. Since you did not share how you call the App component, I can only see 1 way to result this to an issue.

    This is the link in CodeSandbox: Invalid hook call.

    Why? Because of the code below which is wrong:

    ReactDOM.render(App(), rootElement);
    

    It should have been:

    ReactDOM.render(, rootElement);
    

    For more info, you should read Rule of Hooks - React

    Hope this helps!

提交回复
热议问题