question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using ref-property

See original GitHub issue

In my project I’m using ref to get the video-element, but that is no longer working as of 3.0.0 Any chance of making it work again, or to make an alternative way to get the VideoElement-node?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
iwatakeshicommented, Oct 6, 2020

@mnervik I didn’t use React.Component, but I think this is similar to what you are looking for and it works on my end:

export default function App() {
  const ref = useRef<HTMLPlyrVideoElement>()
  const [currentTime, setCurrentTime] = useState('0.00')
  const [id, setId] = useState<NodeJS.Timeout>()

  useEffect(() => {
    const onPlay: PlyrCallback = (event) => {
      setId(
        setInterval(() => {
          const currentTime = ref?.current?.plyr?.currentTime / 100

          setCurrentTime(currentTime.toFixed(2))
        }, 1000)
      )
    }

    const onPause: PlyrCallback = (event) => {
      clearInterval(id)
    }

    const onEnded: PlyrCallback = (event) => {
      clearInterval(id)
      setCurrentTime('0.00')
    }

    ref?.current?.plyr?.on('play', onPlay)
    ref?.current?.plyr?.on('pause', onPause)
    ref?.current?.plyr?.on('ended', onEnded)

    return () => {
      ref?.current?.plyr?.off('play', onPlay)
      ref?.current?.plyr?.off('pause', onPause)
      ref?.current?.plyr?.off('ended', onEnded)
   }
  }, [])

  return <>
    <Plyr ref={ref} />
  <>
}

Note: I just added the types so they are not available yet.

1reaction
iwatakeshicommented, Oct 4, 2020

@mnervik I think I’ve got a fix for it. Try it out by changing your package.json to:

{
  // ...
  "plyr-react": "git+https://github.com/chintan9/plyr-react.git#fix/forward-ref",
}

then run npm update or yarn upgrade (I think)

Let me know if it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refs and the DOM - React
Refs are created using React.createRef() and attached to React elements via the ref attribute. Refs are commonly assigned to an instance property when...
Read more >
A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >
Working with refs in React | CSS-Tricks
createRef () is a new API that shipped with React 16.3. You can create a ref by calling React.createRef() and attaching a React...
Read more >
How to Use React Refs - Ross Bulat - Medium
Refs are created using React.createRef() , and are assigned to class properties. In the above example the ref is named myRef , which...
Read more >
Refs in React js - Topcoder
To add ref to a DOM element we need to pass the ref attribute and in this case, the 'current' property will be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found