-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add eventlisterner in useEffect in CustomPlyrInstance not working with empty dependency array #732
Comments
That's because in that cause you don't have access to the latest You can optimize your workflow by one of the following
useEffect(yourEffect, [ref.current && ref.current.plyr.source])
const [hasPlyr, setHasPlyr] = useState(false)
useEffect(() => {
if (current.plyr.source === null) setHasPlyr(true)
})
useEffect(() => {
/* Your API bounding useEffect */
}, [hasPlyr]); Note: These might not work on the first try and these are the idea to show how it might be possible. |
Thanks for the suggestions. I've tried nearly everything and can't seem to get the plyr instance to play with event handlers. I think this is a weakness of plyr-react. In plain vanilla javascript the ability to call plyr.play() or other methods is so easy. Even the Angular version of plyr I managed to use on a project and it was pretty easy. The original method you showed me works using api.plyr.on, but it calls it multiple times. I'm having trouble limiting it, because when a rerender happens, the audio starts over. |
Thanks for your feedback, @bradrice. The only reason a code improves is through the feedback we get from day-to-day usage. When you do it on js, you have control over the plyr instance you've created yourself, so it makes it easy to interact with. The The goal of plyr-react is to make it totally separated from the plyr package, this is why we don't re-export every method that plyr gives through hard bounded class methods and send you the latest instance of plyr. I guess the other issue which makes it a little bit harder to follow is the Proxy we are using to eliminate future development null checks. Here is a comparison of API calls without first layer proxy function noProxyGetAPI(plyr) {
return () => ({
plyr,
})
} Now you should always check in your code that whether the plyr instance exist or not const onPlayHandler = () => { ref.current.plyr?.play() } If it is possible to make a code sandbox, we can help to eliminate attaching unwanted event listeners. |
thanks for the detailed explanation. |
If I leave off the dependency array, the eventlisteners get attached, but mulitple times due to rerenders of the AudioPlayer component I have created. I would like to prevent adding additional event listeners.
Here is my code in the useEffect
Why can't I pass an empty array to minimize the calls to add the event listeners?
The text was updated successfully, but these errors were encountered: