Javascript 无法读取属性';设置状态';单击按钮时返回空值

Javascript 无法读取属性';设置状态';单击按钮时返回空值,javascript,reactjs,Javascript,Reactjs,我正在开发一个reactjs应用程序,在updateVert函数中访问状态时遇到了这个问题。当我点击“播放”按钮时,actionButton被调用,从actionButton内部,updateVert被调用 下面是我的代码片段 updateVert() { console.log("inside upver") if(play) { cancelAnimationFrame(this.updateVert); re

我正在开发一个reactjs应用程序,在updateVert函数中访问状态时遇到了这个问题。当我点击“播放”按钮时,actionButton被调用,从actionButton内部,updateVert被调用

下面是我的代码片段

updateVert() {

        console.log("inside upver")
        if(play) {
            cancelAnimationFrame(this.updateVert);
            return;
        }

       this.setState({xPos: this.state.xPos + 1}, ()=>{
           if(this.state.xPos >= w) {
               // stop animation...
               this.setState({xPos:0, playing:false});
           }
           ctx.clearRect(0, 0, w, h);

           // draw new one...
           ctx.beginPath();
           ctx.strokeStyle = "#19f";
           ctx.lineWidth = 2;
           ctx.moveTo(this.state.xPos, 0);
           ctx.lineTo(this.state.xPos, 200);
           ctx.stroke();

           if(this.state.playing) {
               console.log("yes true")
               requestAnimationFrame(this.updateVert)
           };
       })
        // reset rectangle content to erase previous line...
    }

    actionButton(){
        if(this.state.playing) {
            // pause...
            this.setState({playing:false},()=>{
                console.log(this.state.playing)
            })
        }
        else {
            this.setState({playing:!this.state.playing},()=>{
                console.log(this.state.playing)
                this.updateVert();
            })

        }
    }

  render() {
        return (
            <div>
                <div className="App">
                    <canvas id="DemoCanvas" width="500" height="200"></canvas>
                </div>
                <button onClick={this.actionButton.bind(this)}>{this.state.playing ? "Stop":"Play"}</button>
            </div>


    );
  }

我正确引用了updateVert函数。我做错了什么?

尝试在requestAnimationFrame

requestAnimationFrame(this.updateVert.bind(this))

希望这有助于

尝试在requestAnimationFrame中专门绑定上下文

requestAnimationFrame(this.updateVert.bind(this))

希望这有助于

绑定上下文

actionButton(){
钩子中,您正在调用
this.updateVert()
,它对
this
没有绑定。因此,在构造函数中绑定它:

this.updateVert.bind(this)

您缺少绑定上下文

actionButton(){
钩子中,您正在调用
this.updateVert()
,它对
this
没有绑定。因此,在构造函数中绑定它:

this.updateVert.bind(this)

它没有获得正确的
上下文。无需为绑定编写额外代码。您需要更改

updateVert() { .... }


它没有获得正确的
上下文。无需为绑定编写额外代码。您需要更改

updateVert() { .... }


requestAnimationFrame,那是什么?我知道,但用于绑定的是什么?任何引用?这对我来说都是一个新发现:)我的建议是,函数本身并不是用箭头函数表示法声明的,所以当调用requestAnimationFrame内部时,它失去了上下文。正如Anshul的回答所建议的,您可以只指定fu我很想知道requestAnimationFrame是如何用于绑定上下文的。仅供参考,我不反对。仅供参考,我不反对。-哈哈,我不在乎:)。正如我所说的,这只是我的建议,需要检查itrequestAnimationFrame,那是什么?我知道,但那用于bin的是什么丁?有参考资料吗?这对我来说是一个新发现:)我的建议是,函数本身并不是用箭头函数表示法声明的,所以当在requestAnimationFrame内部调用时,它丢失了上下文。正如Anshul的回答所建议的,您可以将函数指定为箭头函数,或者可以在其中具体绑定上下文Ius想知道requestAnimationFrame是如何用于绑定上下文的。仅供参考,我不会投反对票。仅供参考,我不会投反对票。-哈哈,我不在乎:)。正如我所说,这只是我的建议,需要检查一下