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.

useEffect for adding api getting recalled on user events

See original GitHub issue

I’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:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
realamirhecommented, Jul 28, 2022

The default value for the deps is null and use following for inner dependency array

 deps​ ​||​ ​[​params​.​options​,​ ​params​.​source​]

I’m somehow losed the context, but think that separating the hooks into multiple useEffect with different dep-array or add custom useEffect 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.

— Reply to this email directly, view it on GitHub https://github.com/chintan9/plyr-react/issues/891#issuecomment-1198169093, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFT42NV5B4ET6RKSF52K5LLVWKFWXANCNFSM52ZZ3LOQ . You are receiving this because you commented.Message ID: @.***>

0reactions
bradricecommented, Jul 28, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

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