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

前端 未结 29 875
花落未央
花落未央 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:54

    I had the same issue, but not with the App. I had a custom class but used a lowercase letter to start the function name and also received the error.

    Changed the first letter of the function name and the export line to CamelCase and error gone.

    in my case the end result was something like:

    function Document() {
    ....
    }
    export default Document;
    

    this solved my problem.

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

    React components (both functional as well as class) must begin with a capital letter. Like

    const App=(props)=><div>Hey</div>
    
    class App extends React.Component{
       render(){
         return <div>Hey</div>
       }
    }
    

    React identifies user-defined components by following this semantic. React's JSX transpiles to React.createElement function which returns an object representation of the dom node. The type property of this object tells whether it is a user-defined component or a dom element like div. Therefore it is important to follow this semantics

    Since useState hook can only be used inside the functional component(or a custom hook) this is the reason why you are getting the error because react is not able to identify it as a user-defined component in the first place.

    useState can also be used inside the custom hooks which is used for the reusability and the abstraction of logic. So according to the rules of hooks, the name of a custom hook must begin with a "use" prefix and must be in a camelCase

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

    the First character of your function should be an Uppercase

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

    Step-1: Change the file name src/App.js to src/app.js

    Step-2: Click on "Yes" for "Update imports for app.js".

    Step-3: Restart the server again.

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

    In JSX, the lower-case tag name is considered as html native component. In order to react recognise the function as React component, need to Capitalized the name.

    Capitalized types indicate that the JSX tag is referring to a React component. These tags get compiled into a direct reference to the named variable, so if you use the JSX <Foo /> expression, Foo must be in scope.
    

    https://reactjs.org/docs/jsx-in-depth.html#html-tags-vs.-react-components

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