[英]react function state is not defined no-undef
I create this function startStop for such button to start and pause a timer.我为这样的按钮创建了这个 function startStop 来启动和暂停计时器。 The error is "Line 108:22: 'sessionLength' is not defined no-undef" (line 108 is the last "this.setState()" in this code block) does it have to do anything with binding?
错误是“第 108:22 行:'sessionLength' 未定义 no-undef”(第 108 行是此代码块中的最后一个“this.setState()”)它与绑定有什么关系吗? what's wrong?
怎么了?
import React, { Component } from 'react';
import './App.css';
class App extends Component {
state = {
breakLength: 5,
sessionLength: 25,
timerState: "paused",
}
constructor(props) {
super(props)
this.startStop = this.startStop.bind(this)
}
startStop() {
if (this.state.timerState === "paused") {
this.setState({
timerState: setInterval(() => decrement(), 1000)
}, function () {console.log(this.state.timerState)})
}
else if (this.state.timerState !== "paused") {
console.log(this.state.timerState)
clearInterval(this.state.timerState)
this.setState({
timerState: "paused"
}, function () {console.log(this.state.timerState)})
}
this.setState({
sessionLength: sessionLength - remaining
}, function () {console.log(this.state.sessionLength)})
}
sessionLength
is a member of this.state
, so to access it you would need to use this.state.sessionLength
. sessionLength
是this.state
的成员,因此要访问它,您需要使用this.state.sessionLength
。
this.setState({
sessionLength: this.state.sessionLength - remaining
}
I think you should declare state inside the constructor我认为您应该在构造函数中声明state
You try:你试试:
constructor(props) {
super(props)
this.state = {
breakLength: 5,
sessionLength: 25,
timerState: "paused",
}
this.startStop = this.startStop.bind(this)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.