简体   繁体   English

如何在React中设置视频端组件的状态?

[英]How can I set the state of a component on video end in react?

In my component I have an componentDidUpdate function where I play a video and on that video I set the video.onended event as noted HERE 在我的组件我有一个componentDidUpdate功能,我播放视频并在该视频中,我设定的video.onended事件作为注意到这里

Currently my code looks like this: 目前,我的代码如下所示:

  componentDidUpdate: function() {
    if(this.state.showVideo){
      this.refs.homeVideo.play();
      // Triggering event on video end
      let homeVideo = document.getElementById("homeVideo");
      homeVideo.onended = function(){
        console.log(this.state);
        this.setState({ showVideo: !this.state.showVideo });
      }
    }
  }

My issue right now is that this.state is undefined in the onended function and so is setState, which is preventing me from updating the state of the component in react so that I can close the video player when it ends. 我现在的问题是onstate函数中未定义this.state,setState也未定义,这使我无法在react中更新组件的状态,以便我可以在视频播放器结束时关闭它。

What is the appropriate react way of handling this? 处理此问题的适当反应方式是什么?

Its because every new function defined its own this value. 这是因为每个新函数都定义了自己的this值。

You can do something like: 您可以执行以下操作:

var self = this;
homeVideo.onended = function(){
  console.log(self.state);
  self.setState({ showVideo: !self.state.showVideo });
}

Or better yet, use arrow functions if your using ES6: 或者更好的是,如果您使用的是ES6,请使用箭头功能:

homeVideo.onended = () => {
   console.log(this.state);
   this.setState({ showVideo: !this.state.showVideo });
}

Arrow functions lexically binds the this value. 箭头函数按词法绑定this值。

You don't need document.getElementById. 您不需要document.getElementById。

Try to update your code to this: 尝试将代码更新为此:

componentDidUpdate: function() {
    var self = this;
    if(this.state.showVideo){
      let video = this.refs.homeVideo;
      video.play();
      video.onended = function(){
        console.log(self.state);
        self.setState({ showVideo: !self.state.showVideo });
      }
    }
  }

JSfiddle example https://jsfiddle.net/ntfjncuf/ JSfiddle示例https://jsfiddle.net/ntfjncuf/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 react 中重定向到另一个组件并传递我在前一个组件中设置的 state? - How can I redirect to another component in react and pass the state that I set in the previous component? 无法在React组件中设置状态 - Can not set a state in a react component 如何在反应 class 组件中设置另一个 state 以将数据从初始 state 传递到 - How can I set another state to pass data to from the initial state in a react class component 由于在设置 state 之前进行组件渲染,我的 React 组件失败了。 我该如何解决这个问题? - My react component is failing due to component rendering before state is set. How can I resolve this issue? 我无法从反应组件值设置 state - I can't set state from react component value React - 如何使用子组件设置我的父 state,使用 function 结构(不是类) - React - How can I set my parent state using child component, using function structures (not classes) 如何仅在设置 state 后渲染反应组件? - How do I render a react component only after a state is set? 我如何使用ES6在React组件中保持状态 - How can i keep state in a React component using ES6 如何确保React组件的状态正确? - How can I ensure that a React component's state is correct? 如何在反应中将组件重置为其初始状态 - How can I reset a component to it's initial state in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM