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

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

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

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

    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

提交回复
热议问题