簡體   English   中英

TypeScript React-通過ref並調用.play()來訪問視頻元素

[英]TypeScript React - access video element by ref and call .play()

我有一個TypeScript React視頻組件,我想在單擊時播放它。 我希望通過使用單擊處理程序來做到這一點,該處理程序通過ref並調用.play()來訪問視頻,但是我遇到了此錯誤:

TS2339: Property 'play' does not exist on type 'RefObject<HTMLVideoElement>'.

這是我的代碼(省略了不相關的部分):

class FullBleedVideo extends React.Component<PropDef, StateDef> {
  private fullBleedVideo: React.RefObject<HTMLVideoElement>;

  constructor(props: PropDef) {
    super(props);

    this.fullBleedVideo = React.createRef();
    this.state = {
      isPlaying: false,
    };
  }

  public handleVideoClick() {
    this.setState({ isPlaying: true });
    this.fullBleedVideo.play();
  }

  render() {
    const { isPlaying } = this.state;

    return (
      <div className="video_container" onClick={this.handleVideoClick.bind(this)}>
        <video
          ref={this.fullBleedVideo}
        >
          <source src={src} type="video/mp4" />
        </video>
      </div>
    );
  }
}

我是TypeScript的新手,但是不確定我哪里出錯了?

this.fullBleedVideo.current.play(); 將工作。 您可以通過current屬性訪問DOM。

https://reactjs.org/docs/refs-and-the-dom.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM