简体   繁体   中英

can anyone help me with this react error?

ERROR

Line 6:44: React Hook "useState" is called in function "app" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks

Search for the keywords to learn more about each error.

I ran into this one with typescript, changing the component name to start with capital letter fixed this for me.

You are using the useState hook and not importing React and the useState Hook in your function

import React,{useState} from "react";

//use a capital letter in the begging of your component name
export const YourComponent = () =>{
   //Now you can use the useState hook here in a react functional component
   const [personsState, setPersonsState ] = useState({ persons: [ 
        {name: 'Mann', age:'20'}, 
        {name: 'Mab', age: '25'}, 
        {name: 'Bar', age: '43'} 
      ] })

   return(
     <div>
       //you can use personsState here now
     </div>
   )
}

create-react-app version 3+

& newer React versions require components

containing React Hooks to be capitalized (PascalCase).

Meaning: The component name & its export (name).

Change the two (names) app to App.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM