Skip to content

Instantly share code, notes, and snippets.

@jbowa
Last active April 21, 2021 15:21
Show Gist options
  • Save jbowa/3049c2ba77b7d619260d29ca2e2db110 to your computer and use it in GitHub Desktop.
Save jbowa/3049c2ba77b7d619260d29ca2e2db110 to your computer and use it in GitHub Desktop.
import {useEffect} from 'react'
import Plyr, {SourceInfo, Options} from 'plyr'
import 'plyr/dist/plyr.css'
const usePlyr = (id: string, sources: SourceInfo, opts?: Options) => {
useEffect(() => {
const el = document.getElementById(id)
const player = new Plyr(el, opts)
player.source = sources
return () => player.destroy()
}, [sources, opts])
}
export default usePlyr
// use
const MediaBlock = () => {
const sources = {
type: 'video',
sources: [{src: sourceUrl}]
}
usePlyr('plyr', sources, options)
render = (
<Container>
<video id='plyr' />
</Container>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment