Skip to content

React setState后值不能立即更新

Posted on:2016年6月2日 at 16:45

setPasswordFirst: function (password)
  {

    this.setState({
      passwordFirst: password
    }, function(){this.check()});

  },
  check: function (){
    this.refs.passwordAgainCheck.sameCheckAgain(this.state.passwordFirst, this.state.passwordSecond);
  },
  setPasswordSecond: function (password)
  {
    this.setState({
      passwordSecond: password
    }, function(){this.check()});
  },
this.setState的值是不会立即更新的,如果在this.setState()后面再输出this.state.passwordFirst,值还是原来的。这时候利用setState的回调函数即可。