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

前端 未结 29 1007
花落未央
花落未央 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条回答
  •  萌比男神i
    2020-11-30 00:45

    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.

提交回复
热议问题