useEffect for adding api getting recalled on user events
See original GitHub issueI’m having an issue with the useRef where I setup an api to call play after my source is ready. However, I also have a control on my page to increase or decrease the font size on the page, and for some reason the audio stops and restarts at the beginning. I’ve determned the useEffect is getting called again on that event. I cannot seem to figure out how to not call the useEffect and still get my player to play at the right time. If I add a dependencies array the useEffect does not start the player. If I remove them, the player starts properly, but then the events on the page cause it to be called again.
Here is my useEffect in my audio player using plyr usePlyr.
// Do all api access here, it is guaranteed to be called with the latest plyr instance
useEffect(() => {
const { current } = ref as React.MutableRefObject<APITypes>;
const api = current as { plyr: PlyrInstance };
console.log(
"useEffect in AudioPlayer",
isPlaying,
hasBookmarkChoice,
ref,
api
);
const onEnded = (e: any) => {
eventBus.dispatch("audioPlayEnd", { message: "audio ended" });
};
const onCanPlay = (e: any) => {
if (isPlaying && hasBookmarkChoice) {
console.log("Audio should be ready to play");
const promise = api.plyr.play();
if (promise) {
promise
.then(() => {
// put up an icon or do ui work for play started here
console.log("Play started");
})
.catch((error) => {
console.log("an error starting the audio occured", error);
});
}
} else {
console.log("Audio paused");
api.plyr.pause();
}
};
const onReady = (e: any) => {
console.log("I'm ready", e);
// api.plyr.play();
};
if (current.plyr.source === null) {
return;
} else {
api.plyr.on("ready", onReady);
api.plyr.on("canplay", onCanPlay);
api.plyr.on("ended", onEnded);
return () => {
api.plyr.off("ended", onEnded);
api.plyr.off("ready", onReady);
api.plyr.off("canplay", onCanPlay);
// clearTimeout(timeId);
};
}
});
Using 5.0.2
Issue Analytics
- State:
- Created a year ago
- Comments:5
Top Results From Across the Web
The last guide to the useEffect Hook you'll ever need
With useEffect , you invoke side effects from within functional components, which is an important concept to understand in the React Hooks era....
Read more >Separating Events from Effects - React Docs
This section describes an experimental API that has not yet been added to React, so you can't use it yet. Use a special...
Read more >How To Call Web APIs with the useEffect Hook in React
In this tutorial, you'll use the useEffect and useState React Hooks to fetch and display information in a sample application, ...
Read more >Re-calling react hook "useEffect" problem - Stack Overflow
Your useEffect hook only fires when isSubmit = true . When you call submitInput you only change the value of isSubmit to !isSubmit...
Read more >Learn React #9: UseEffect & UseEffect with API Requests
In this video we go over how to use React's useEffect hook complete with translations to componentDidMount, componentDidUpdate, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The default value for the deps is null and use following for inner dependency array
I’m somehow losed the context, but think that separating the hooks into multiple
useEffect
with different dep-array or add customuseEffect
for first render that change src can be helpful. Let me know, if these could solve your solution.On Thu, Jul 28, 2022, 18:19 Brad Rice @.***> wrote:
Wow, after banging my head for weeks, that worked. I just passed an empty array as the third argument. Except, now the audio source doesn’t get updated when I change pages. I’m not sure what I need to pass in the dependencies array.