Based on the React documentation that I looked at, I think I would use "useRef" to update a function, but I'm not sure. I want to fire an event to mute a video there is a scroll event. Right now I'm using onClick to play/pause the video.
Video.js
import React, { useRef, useState } from "react";
import "./Video.css";
function Video({ url }) {
const [muted, setMuted] = useState(true);
const [playing, setPlaying] = useState(false);
const videoRef = useRef(null);
var Chrome =
navigator.userAgent.includes("Chrome") &&
navigator.vendor.includes("Google Inc");
const onVideoPress = () => {
if (playing && !Chrome) {
videoRef.current.pause();
setPlaying(false);
setMuted(true);
} else if (playing && Chrome) {
videoRef.current.pause();
setPlaying(false);
setMuted(false);
} else {
videoRef.current.play();
setPlaying(true);
setMuted(false);
}
};
return (
<div className="video">
<video
className="video__player"
autoPlay={true}
loop={true}
muted={muted}
playsInline={true}
// controls
ref={videoRef}
onClick={onVideoPress}
>
<source className="video__controls" src={url} type="video/mp4"></source>
</video>
{/* <VideoFooter /> */}
</div>
);
}
export default Video;
This is not the same How to add scroll event in react component because I am not extending the function as a component.