Lanttcat / Vimer

blog by Issues
0 stars 0 forks source link

React setState()更新过程 #1

Open Lanttcat opened 5 years ago

Lanttcat commented 5 years ago

先抛出来一个demo


// After click, show number should be 3

class App extends React.Component {
  state = {
    total: 0
  }
  handle = () => {
    this.setState({
      total: 2
    });
  }
  testFunc = (param) => {
    const nu = param || this.state.total;
    return nu + 1;
  }

  render() {
    return (
      <div className="App">
        <h1 onClick={this.handle}>Hello CodeSandbox</h1>
        <h2>{this.testFunc()}</h2>
      </div>
    );
  }
}```