【发布时间】:2021-04-19 19:37:57
【问题描述】:
我正在尝试使用 React.js 中的 ref 来控制视频的播放/暂停状态,我的代码可以正常工作,但我正在尝试解决 tslint 错误:
function App() {
const playVideo = (event:any) => {
video.current.play()
}
const video = useRef(null)
return (
<div className="App">
<video ref={video1} loop src={bike}/>
</div>
);
}
这会导致
TS2531: Object is possibly 'null'.
所以我尝试改变const video = useRef(null)
给const video = useRef(new HTMLVideoElement())
我得到:
TypeError: Illegal constructor
我也试过:const video = useRef(HTMLVideoElement)
这导致:
TS2339: Property 'play' does not exist on type '{ new (): HTMLVideoElement; prototype: HTMLVideoElement; }'
【问题讨论】:
-
@Mike'Pomax'Kamermans
useRef是createRef的钩子版本。
标签: html reactjs typescript dom use-ref