this.setState()
to manipulate state
import React, {Component} from 'react';
class App extends Component {
// SETTING UP STATE
state = {
persons: [
{name: 'Brad', age: 22},
{name: 'Ryan', age: 21}
]
}
render() {
return(
...
)
}
}
export default App;
import React, {Component} from 'react';
class App extends Component {
// SETTING UP STATE
...
render() {
return(
<div>
// ACCESSING STATE
<Person name={this.state.props.persons[0].name}
age={this.state.props.persons[0].age} />
<Person name={this.state.props.persons[1].name}
age={this.state.props.persons[1].age} />
</div>
)
}
}
export default App;
import React, {Component} from 'react';
class App extends Component {
// SETTING UP STATE
...
// EVENT HANDLER
switchNameHandler = () => {
// MANIPULATING STATE
this.setState({
persons: [
{name: 'John', age: 20},
{name: 'Adam', age: 22}
]
})
}
render() {
return(
<div>
// EVENT LISTENER
<button onClick={this.switchNameHandler}>Switch Names</button>
...
</div>
)
}
}
export default App;