Javascript 设置状态在React中的设置间隔内不工作

Javascript 设置状态在React中的设置间隔内不工作,javascript,react-native,ecmascript-6,react-jsx,Javascript,React Native,Ecmascript 6,React Jsx,当我单击调用_handlePressStartStop()函数的按钮时,我得到一个关于setState的错误。它告诉我们: **(intermediate value).bind is not a function** 我知道我在这方面遇到了麻烦,也许这是我问题的根源。这是我的密码: class StopWatch extends Component { constructor(props) { super(props); this.state = { startTime

当我单击调用_handlePressStartStop()函数的按钮时,我得到一个关于setState的错误。它告诉我们:

 **(intermediate value).bind is not a function**
我知道我在这方面遇到了麻烦,也许这是我问题的根源。这是我的密码:

class StopWatch extends Component {

constructor(props) {
  super(props);

  this.state = {
    startTime: null,
    timeElapsed: null
  };
}

render() {
  return (
    <View style={styles.container}>
      <View style={styles.header}>
        <View style={styles.counter}>
          <Text style={styles.counterText}>{this.state.timeElapsed}</Text>
        </View>
        <View style={styles.buttonsWrapper}>
          <Button buttonType='startStop' onPress={this._handlePressStartStop}>Start</Button>
          <Button buttonType='lap' onPress={this._handlePressLap}>Lap</Button>
        </View>
      </View>
      <View style={styles.footer}>
      </View>
    </View>
  )
}

_handlePressStartStop() {
  console.log("press start");

  let startTime = new Date();

  setInterval(() => {
      this.setState({
        timeElapsed: new Date() - startTime
      }.bind(this))
  }, 1000);

}

_handlePressLap() {
  console.log("press lap");
}
类秒表扩展组件{
建造师(道具){
超级(道具);
此.state={
开始时间:空,
已用时间:空
};
}
render(){
返回(
{this.state.timeappeased}
开始
大腿部
)
}
_handlePressStartStop(){
控制台日志(“按下启动”);
让startTime=新日期();
设置间隔(()=>{
这是我的国家({
已用时间:新日期()-startTime
}.绑定(本)
}, 1000);
}
_扶手{
控制台日志(“压圈”);
}
}试试看
这个。\u handlePressStartStop.bind(这个)

你不想那样绑定到这个。您正在将对象绑定到此对象,这是无效的。绑定处理程序怎么样

onPress={this.\u handlePressStartStop}.bind(this)


这样,处理程序函数中执行的所有操作都将具有相同的
this
(react组件实例)

对象没有
.bind
方法。这就是错误告诉你的<代码>{…}.bind(此)完全无效。相关:谢谢,它正在工作!