I'm trying to control the play/pause state of a video using ref's in React.js, my code works but there are tslint errors I am trying to work through:
function App() {
const playVideo = (event:any) => {
video.current.play()
}
const video = useRef(null)
return (
<div className="App">
<video ref={video1} loop src={bike}/>
</div>
);
}
This will cause
TS2531: Object is possibly 'null'.
So I try to change const video = useRef(null)
to const video = useRef(new HTMLVideoElement())
and I get:
TypeError: Illegal constructor
I have also tried: const video = useRef(HTMLVideoElement)
which results in:
TS2339: Property 'play' does not exist on type '{ new (): HTMLVideoElement; prototype: HTMLVideoElement; }'
<video>
element. Don't use thesrc
attribute, use <source> elements. Also, if this is actual React, use createRef to create a ref. – BerkeuseRef
is the hook version ofcreateRef
. – Bergson