1import React, {Component} from 'react';
2
3class ButtonCounter extends Component {
4 constructor() {
5 super()
6 // initial state has count set at 0
7 this.state = {
8 count: 0
9 }
10 }
11
12 handleClick = () => {
13 // when handleClick is called, newCount is set to whatever this.state.count is plus 1 PRIOR to calling this.setState
14 let newCount = this.state.count + 1
15 this.setState({
16 count: newCount
17 })
18 }
19
20 render() {
21 return (
22 <div>
23 <h1>{this.state.count}</h1>
24 <button onClick={this.handleClick}>Click Me</button>
25 </div>
26 )
27 }
28}
29
30export default ButtonCounter
31
1// Correct
2this.setState(function(state, props) {
3 return {
4 counter: state.counter + props.increment
5 };
6});
1 const [a, b] = React.useState(['hi','world']);
2 const dup = [...a]; //won't work without spread operator
3 b(dup);