how to play/pause video in React without external library?

后端 未结 5 1038

I have a video tag () in my webpage, and a \"play/pause\" button that when the user clicks on it, the video starts/stops playing . How can I do so in react if I\'m not allowed t

5条回答
  •  春和景丽
    2021-02-14 00:35

    Use ref attribute to create a link to the video and using that reference we can able to use video controls on the video component

    Try this code,

    import React from "react";
    class VideoDemo extends React.Component {
      
      getVideo = elem => {
        this.video = elem
      }
    
      playVideo = () => {
        this.video.play()
      };
    
      pauseVideo = () => {
        this.video.pause();
      };
    
      render = () => {
        return (
          
    ); }; } export default VideoDemo;

提交回复
热议问题