React.js having state based on other state

前端 未结 3 1161
悲&欢浪女
悲&欢浪女 2021-02-01 16:22

I\'m running into some problems with React.js and the state not being immediately set when calling setState(). I\'m not sure if there are better ways to approach this, or if it

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 16:53

    Unsure if this was the case when question was asked, though now the second parameter of this.setState(stateChangeObject, callback) takes an optional callback function, so can do this:

    setAlarmTime: function(time) {
      this.setState({ alarmTime: time }, this.checkAlarm);
    },
    checkAlarm: function() {
      this.setState({
        alarmSet: this.state.alarmTime > 0 && this.state.elapsedTime < this.state.alarmTime
      });
    }, ...
    

提交回复
热议问题