-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sample code does not work when interacting with component states [video.js with react] #7361
Comments
👋 Thanks for opening your first issue here! 👋 If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can. |
It's not the event that's the issue. It's whatever |
@decision4fr could you add a snippet? I can't see you interact with the player events. What does the |
@gkatsev I understand that the error comes from line 23, and I don't think the |
getTargetElement () is equivalent to videoRef.current |
You should notice that To fix this problem, you can try these:
return (
<div>
<h3>{saludo}</h3>
<VideoHtml key="video" />
</div>
)
const VideoHtml = React.useCallback((props) => (
<div data-vjs-player>
<video ref={videoRef} muted className="video-js vjs-big-play-centered" />
</div>
), []);
import React from "react";
import videojs from "video.js";
const VideoHtml = React.forwardRef((props, videoRef) => (
<div data-vjs-player>
<video ref={videoRef} muted className="video-js vjs-big-play-centered" />
</div>
));
const VideoJsPlayer = (props) => {
const videoRef = React.useRef(null);
const [saludo, setSaludo] = React.useState("hola");
React.useEffect(() => {
const videoElement = videoRef.current;
let player;
if (videoElement) {
player = videojs(videoElement, props, () => {
console.log("player is ready 123123");
});
player.on("play", () => {
console.log("desde play");
setSaludo("Hola mundo");
});
}
return () => {
if (player) {
player.dispose();
}
};
}, [props]);
return (
<div>
<h3>{saludo}</h3>
<VideoHtml ref={videoRef} />
</div>
);
};
export default VideoJsPlayer; |
@FredZeng Thanks for your answer, I had already tried to use I add another snippet with the mentioned error. https://codesandbox.io/s/blazing-worker-jmtyy?file=/src/VideoPage.js |
@jomarquez21 Well, I get your point now. If you dive into video.js dispose() {
// ...
// **What makes errors happened**
if (this.el_) {
// Remove element from DOM
if (this.el_.parentNode) {
this.el_.parentNode.removeChild(this.el_);
}
this.el_ = null;
}
// remove reference to the player after disposing of the element
this.player_ = null;
} The To fix this, you can try, or make sure import React from "react";
import videojs from "video.js";
import VideoHtml from "./VideoHtml";
const VideoJsPlayer = (props) => {
const videoRef = React.useRef(null);
const playerRef = React.useRef(null);
const [saludo, setSaludo] = React.useState("hola");
React.useEffect(() => {
const videoElement = videoRef.current;
if (videoElement) {
if (!playerRef.current) {
playerRef.current = videojs(videoElement, props, () => {
console.log("player is ready 123123");
});
playerRef.current.on("play", () => {
console.log("desde play");
setSaludo("Hola mundo");
});
} else {
// only update necessary props
playerRef.current.width(props.width);
playerRef.current.height(props.height);
playerRef.current.autoplay(props.autoplay);
playerRef.current.controls(props.controls);
playerRef.current.src(props.sources);
}
}
}, [props]);
React.useEffect(() => {
return () => {
if (playerRef.current) {
playerRef.current.dispose();
playerRef.current = null;
}
};
}, []);
return (
<div>
<h3>{saludo}</h3>
<VideoHtml ref={videoRef} />
</div>
);
};
export default VideoJsPlayer; |
Any help to improving the docs would be greatly appreciated! |
I think this issue can already be closed. |
Improve documentation on functional components to clean up reference if the component is unmounted. Fixes videojs#7361
Description
When trying to update a state in any event related to the player, the video disappears. https://codesandbox.io/s/videojs-react-hooks-example-forked-17bx0?file=/src/VideoJsPlayer.js
Steps to reproduce
Explain in detail the exact steps necessary to reproduce the issue.
player = videjs(reference, options);
player.on('play', onPlayerListener);
Results
Expected
I expected to be able to interact with the component through the events of the player.
Actual
When the play button is clicked the player disappears.
Error output
No error is displayed on the screen
Additional Information
Please include any additional information necessary here. Including the following:
versions
videojs
7.14.3
browsers
Any
OSes
Any
plugins
n/a
react
17.0.2
react dom
17.0.2
The text was updated successfully, but these errors were encountered: