I am currently going through an intro React course and get the following error when trying to compile my code:
src/App.js
Line 6:5: 'state' is not defined no-undef
Here is the code from my main React component:
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;
I have tried everything from copying the instructor's code exactly, to looking at other answers on Stack, but to no avail. Weirdly, the page loads correctly for a second, only to raise an error.
Any help would be hugely appreciated. Thank you.
Since you are using Class components, you have to add this
. So it becomes this.state
Also check out this guide manage state in react
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.