To solve the error "React Hook 'useState' is called in function that is neither a React function component nor a custom React Hook function", uppercase the first letter of the function name or prefix the function name with use , e.g. useCounter to make it a component or a custom hook.
The React useState Hook allows us to track state in a function component. State generally refers to data or properties that need to be tracking in an application.
Component names must start with a capital letter because this helps React differentiate between built-in elements like p , div , span and components we define. What is this? Like the documentation states: Only call hooks from React function components or from custom hooks.
useState is a Hook that allows you to have state variables in functional components. You pass the initial state to this function and it returns a variable with the current state value (not necessarily the initial state) and another function to update this value.
Try to capitalize 'app' like
const App = props => {...}
export default App;
In React, components need to be capitalized, and custom hooks need to start with use
.
I feel like we are doing the same course in Udemy.
If so, just capitalize the
const app
To
const App
Do as well as for the
export default app
To
export default App
It works well for me.
As far as I know a linter is included into the this package. And it requires you componend should begin from Capital character. Please check it.
However as for me it's sad.
Use first letter capital in the function name.
function App(){}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With