Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class People extends Component {
- constructor(props) {
- super(props);
- this.state = {
- people:[{ name: "mary", age: 25 }, { name: "john", age: 30 }]
- };
- }
- updateJohnAge = ()=>{
- const people = [...this.state.people];
- people[1].age=31;
- this.setState({people});
- //This will not work.
- //People is a new array, but john is the same object
- }
- render() {
- return (
- <div>
- <Person person={this.state.people[1]}></Person>
- <button onClick={this.updateJohnAge}>Set john age to 31</button>
- </div>
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement