I\'m trying to use react hooks for a simple problem
const [personState,setPersonState] = useState({ DefinedObject });
with following depend
You are getting this error: "React Hook "useState" is called in function "App" which is neither a React function component or a custom React Hook function"
Solution: You basically need to Capitalize the function.
For example:
const Helper =()=>{}
function Helper2(){}
The solution, as Yuki already pointed, is to capitalize the component name. It's important to note that not only the "default" App component needs to be capitalized, but all components:
const Person = () => {return ...};
export default Person;
This is due to eslint-plugin-react-hooks package, specifically isComponentName() function inside RulesOfHooks.js script.
Official explanation from Hooks FAQs:
We provide an ESLint plugin that enforces rules of Hooks to avoid bugs. It assumes that any function starting with ”use” and a capital letter right after it is a Hook. We recognize this heuristic isn’t perfect and there may be some false positives, but without an ecosystem-wide convention there is just no way to make Hooks work well — and longer names will discourage people from either adopting Hooks or following the convention.
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.
React components names should be capitalized and custom hooks functions should start with the use keyword to identify as a react hook function.
So, capitalize your app components to App
Replace this
export default app;
with this
export default App;
This is because of the ESLint rule for React Hooks. Find the link of the rule here:
ESLint rule for React Hooks
As of now, the rule is described in the line no. 44.