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

前端 未结 29 876
花落未央
花落未央 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条回答
  • 2020-11-30 00:38
    React Hook "useState" is called in function "App" which is neither a React function component or a custom React Hook function"
    

    For the following error , capitalize the component first letter like, and also the export also.

    const App  = props => {
    ...}
    export default App;
    
    0 讨论(0)
  • 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(<App />, rootElement);
    

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

    Hope this helps!

    0 讨论(0)
  • 2020-11-30 00:40

    Capitalize the app to App will surely work.

    0 讨论(0)
  • 2020-11-30 00:41

    I had the same issue. turns out that Capitalizing the "A" in "App" was the issue. Also, if you do export: export default App; make sure you export the same name "App" as well.

    0 讨论(0)
  • 2020-11-30 00:42

    In the app function you have incorrectly spelled the word setpersonSate, missing the letter t, thus it should be setpersonState.

    Error:

    const app = props => { 
        const [personState, setPersonSate] = useState({....
    

    Solution:

    const app = props => { 
            const [personState, setPersonState] = useState({....
    
    0 讨论(0)
  • 2020-11-30 00:43

    Components should start with capital letters. Also remember to change the first letter in the line to export!

    0 讨论(0)
提交回复
热议问题