Last active
August 23, 2018 19:26
-
-
Save drenther/925ae4707da10c790d2b38da626da3c3 to your computer and use it in GitHub Desktop.
src/components/Video.js for react-page-visibility-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
class Video extends Component { | |
videoRef = React.createRef(); | |
// Safari won't allow playing the video without user interaction | |
componentDidUpdate() { | |
const video = this.videoRef.current; | |
if (this.props.active) video.play(); | |
else video.pause(); | |
} | |
render() { | |
return ( | |
<video autoPlay ref={this.videoRef}> | |
<source type="video/mp4" src={this.props.src} /> | |
</video> | |
); | |
} | |
} | |
export default Video; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment