我的网页上有一个视频标签(),并且播放/暂停"按钮,当用户点击它时,视频开始/停止播放。如果我不允许使用j来调用" getElementById"我怎么能做出反应呢?然后使用play()/ pause()内置方法。 任何的想法?
答案 0 :(得分:11)
最直接的方法是使用refs
这是一个React功能,它允许您调用从render()
返回的组件实例上的方法。
您可以在文档中详细了解它们:https://facebook.github.io/react/docs/more-about-refs.html
在这种情况下,只需在ref
标记中添加video
字符串即可:
<video ref="vidRef" src="some.mp4" type="video/mp4"></video>
当您向按钮添加点击处理程序时:
<button onClick={this.playVideo.bind(this)}>PLAY</button>
playVideo
方法可以通过refs
访问您的视频参考:
playVideo() {
this.refs.vidRef.play();
}
这是一个有效的DEMO,因此您可以查看完整的示例。
答案 1 :(得分:8)
React功能组件的更新示例:
import React, { useRef} from 'react'
function myComponent(props) {
const vidRef = useRef(null);
const handlePlayVideo = () => {
vidRef.current.play();
}
return (
<video ref={vidRef}>
<source src={[YOUR_SOURCE]} type="video/mp4" />
</video>
)
}
答案 2 :(得分:1)
此答案添加到@mheavers中,我对此表示赞成。
有一些区别:
noControls
作为道具传递给Video
组件,并且仅在<video>
没有默认控件(即noControls
通过的情况)。video__play-button
创建播放按钮覆盖样式,而相同的 handler 可以通过类is-playing
隐藏它。ref
并将其作为参数传递给纯渲染函数。import React, { useRef } from 'react';
import PropTypes from 'prop-types';
const renderVideo = ({
noControls,
src,
vidButtonRef,
vidRef,
handleToggleVideo,
}) => (
<>
{noControls ? (
<div ref={vidButtonRef} className="video video__play-button">
<video
ref={vidRef}
src={src}
onClick={handleToggleVideo}
></video>
</div>
) : (
<video
src={src}
controls
controlsList="nodownload"
></video>
)}
</>
);
const Video = props => {
const vidRef = useRef(null);
const vidButtonRef = useRef(null);
const { noControls, src } = props;
const handlePlay = () => {
vidRef.current.play();
// hide overlay play button styles, set by 'video__play-button'
vidButtonRef.current.classList.add('is-playing');
};
const handlePause = () => {
vidRef.current.pause();
// show overlay play button styles, set by 'video__play-button'
vidButtonRef.current.classList.remove('is-playing');
};
const handleToggleVideo = () => (vidRef.current.paused ? handlePlay() : handlePause());
return (
<>
{renderVideo({
noControls,
src,
vidButtonRef,
vidRef,
handleToggleVideo,
})}
</>
);
};
Video.propTypes = {
noControls: PropTypes.bool,
videoUrl: PropTypes.string,
};
export default Video;
答案 3 :(得分:1)
使用ref属性创建到视频的链接,使用该引用,我们可以在视频组件上使用视频控件
尝试此代码,
import React from "react";
class VideoDemo extends React.Component {
getVideo = elem => {
this.video = elem
}
playVideo = () => {
this.video.play()
};
pauseVideo = () => {
this.video.pause();
};
render = () => {
return (
<div>
<video
ref={this.getVideo}
src="http://techslides.com/demos/sample-videos/small.mp4"
type="video/mp4"
/>
<div>
<button onClick={this.playVideo}>
Play!
</button>
<button onClick={this.pauseVideo}>
Pause!
</button>
</div>
</div>
);
};
}
export default VideoDemo;
答案 4 :(得分:0)
如果您想使用 ES6
,则接受的答案使用的是旧的响应样式自动播放暂停以及手动播放Polestar简介的手动控件的简单组件:
import React from "react";
class Video extends React.Component {
componentDidMount = () => {
this.playVideo();
};
componentWillUnmount = () => {
this.pauseVideo();
};
playVideo = () => {
// You can use the play method as normal on your video ref
this.refs.vidRef.play();
};
pauseVideo = () => {
// Pause as well
this.refs.vidRef.pause();
};
render = () => {
return (
<div>
<video
ref="vidRef"
src="https://assets.polestar.com/video/test/polestar-1_09.mp4"
type="video/mp4"
/>
<div>
<button onClick={this.playVideo}>
Play!
</button>
<button onClick={this.pauseVideo}>
Pause!
</button>
</div>
</div>
);
};
}
export default Video;