chintan9 / plyr-react

A simple, accessible and customisable react media player for Video, Audio, YouTube and Vimeo
https://github.com/chintan9/plyr-react
MIT License
475 stars 52 forks source link

Question: How do I use setters with a ref? #998

Open Johannes5 opened 1 year ago

Johannes5 commented 1 year ago

Plyr has setters and getters:

image

I would like to use shortcuts to change the playing speed. According to the above examples, with plyr this would be done with player.speed = 1.8

But since I am using the forwarded ref from a custom plyr instance I've tried it like this:

image

I've gotten the error message that the left-hand side of the argument is invalid and realized that setting on a ref is pointless

So do I use plyrs setters in my situation?

realamirhe commented 1 year ago

Hey @Johannes5

The speed & volume are attributes that options props received, so you can store them in the state (or ref) conditionally and re-render your component with new speed. Is that solve your problem?

Johannes5 commented 1 year ago
image

With the above code I wasn't able to set the speed.

So far I was only able to set the speed inside the useEffect() inside CustomPlyrInstance via api.plyr.speed = 1.75

Because I want to change the speed via Hotkeys - and I'm using a useHotkeys hook - I need to access this plyr instance outside the useEffect Hook.

Which begs the question - why the useEffect hook in the first place? And - what can I do?