我正在尝试使用React Hooks解决一个简单的问题
const [personState,setPersonState] = useState({ DefinedObject });
具有以下依赖项。
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.0"
}
但我仍然收到以下错误:
./src/App.js
第7行:
React Hook“useState”在函数“app”中被调用,而它既不是React函数组件也不是自定义的React Hook函数 react-hooks/rules-of-hooks第39行:
'state'未定义
no-undef搜索关键词以了解有关每个错误的更多信息。
组件代码如下:
import React, {useState} from 'react';
import './App.css';
import Person from './Person/Person';
const app = props => {
const [personState, setPersonSate] = useState({ person:[ {name:'bishnu',age:'32'}, {name:'rasmi',age:'27'}, {name:'fretbox',age:'4'} ], });
return (
<div className="App">
<h2>This is react</h2>
<Person name={personState.person[1].name} age="27"></Person>
<Person name={personState.person[2].name} age="4"></Person>
</div> );
};
export default app;
人物组件
import React from 'react';
const person = props => {
return(
<div>
<h3>i am {props.name}</h3>
<p>i am {props.age} years old</p>
<p>{props.children}</p>
</div>
)
};
export default person;
], }); return ( <div className="App"> <h2>这是React</h2>
<Person name={personState.person[1].name} age="27"></Person> <Person name={personState.person[2].name} age="4"></Person> </div> ); };export default app; - Bishnu
)
} export default person; - Bishnu