我目前正在学习 React 入门课程,并在尝试编译我的代码时遇到以下错误:
src/App.js
Line 6:5: 'state' is not defined no-undef
这是我的主要 React 组件的代码:
import React from 'react';
import './App.css';
import Person from './Person/Person.js';
class App extends React.Component {
state = {
persons: [
{ name: 'Max', age: 28 },
{ name: 'Manu', age: 29 },
{ name: 'Stephanie', age: 26 }
],
otherState: 'some other value'
}
render() {
return (
<div className="App">
<h1>Does this work now?</h1>
<p>This is really working</p>
<button>Switch Name</button>
<Person name={this.state.persons[0].name} age={this.state.persons[0].age}> My Hobbies: Racing</Person>
<Person name={this.state.persons[1].name} age={this.state.persons[1].age}> My Hobbies: Racing</Person>
<Person name={this.state.persons[2].name} age={this.state.persons[2].age}> My Hobbies: Racing</Person>
</div>
);
// return React.createElement('div', {className: 'App'}, React.createElement('h1', null, 'does this work now'));
}
}
export default App;
我尝试了从完全复制讲师的代码到在 Stack 上查看其他答案的所有方法,但都无济于事。奇怪的是,页面正确加载了一秒钟,却引发了错误。
任何帮助将不胜感激。 谢谢。