Skip to content

Instantly share code, notes, and snippets.

@drenther
Last active August 23, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drenther/925ae4707da10c790d2b38da626da3c3 to your computer and use it in GitHub Desktop.
Save drenther/925ae4707da10c790d2b38da626da3c3 to your computer and use it in GitHub Desktop.
src/components/Video.js for react-page-visibility-example
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