I\'m trying to use react hooks for a simple problem
const [personState,setPersonState] = useState({ DefinedObject });
with following depend
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.
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
the First character of your function should be an Uppercase
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.
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