<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: cam 🙂</title>
    <description>The latest articles on DEV Community by cam 🙂 (@campedersen).</description>
    <link>https://dev.to/campedersen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F363119%2F108d3781-73ee-46e4-bfd2-710029172ef2.png</url>
      <title>DEV Community: cam 🙂</title>
      <link>https://dev.to/campedersen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/campedersen"/>
    <language>en</language>
    <item>
      <title>Sharing Audio in React with useContext</title>
      <dc:creator>cam 🙂</dc:creator>
      <pubDate>Tue, 21 Apr 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/campedersen/sharing-audio-in-react-with-usecontext-53mn</link>
      <guid>https://dev.to/campedersen/sharing-audio-in-react-with-usecontext-53mn</guid>
      <description>&lt;p&gt;I ❤️ React Hooks, and my favorite lately is &lt;code&gt;useContext&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I was working with &lt;a href="https://ykyz.com"&gt;YKYZ&lt;/a&gt; recently, an audio-based social network. They have a feature that lets you listen to all Bleats (audio uploads) one after the other. But the mechanism was breaking.&lt;/p&gt;

&lt;p&gt;Most browsers now disable autoplaying audio by requiring a user interaction to &lt;code&gt;play()&lt;/code&gt; an &lt;code&gt;Audio&lt;/code&gt; instance. I figured the bug had something to do with this, so after confirming the behavior on several browsers I started digging into the code. I noticed that every &lt;code&gt;Bleat&lt;/code&gt; component had its own &lt;code&gt;Audio&lt;/code&gt; instance, and the autoplay was coordinated by a higher-level component.&lt;/p&gt;

&lt;p&gt;After reading some MDN docs, I realized that most browsers must implement the interaction component per-&lt;code&gt;Audio&lt;/code&gt; rather than per-pageload, which was an assumption in the existing code. I threw together a tiny proof of concept to see if detecting the end of the clip, swapping the &lt;code&gt;src&lt;/code&gt;, and playing again could get around the interaction requirement, and it did. But how to implement a shared audio context in React?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6YzVTli0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://campedersen.com/images/jim-and-pam.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6YzVTli0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://campedersen.com/images/jim-and-pam.jpg" alt="Sharing audio like Jim and Pam"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;center&gt;&lt;small&gt;Sharing audio like Jim and Pam&lt;/small&gt;&lt;/center&gt;
&lt;h2&gt;
  
  
  Context vs Props
&lt;/h2&gt;

&lt;p&gt;Most times when a codebase needs to share state, people jump to &lt;a href="https://redux.js.org/"&gt;Redux&lt;/a&gt;. As a quick overview, Redux implements the Dispatcher pattern from &lt;a href="https://facebook.github.io/flux/docs/dispatcher"&gt;Flux&lt;/a&gt; - basically just a JSON store, but funneling all the mutations to that store through a central point to ensure all those different mutations are exlicitly defined. &lt;a href="https://react-redux.js.org/"&gt;react-redux&lt;/a&gt; is then used to pass this JSON store and dispatcher down through React Context, and &lt;code&gt;connect&lt;/code&gt; is used to patch this context into the component’s props.&lt;/p&gt;

&lt;p&gt;None of this is really necessary, especially just to share an Audio instance. Also, Redux wasn’t in the codebase I was using and this wasn’t the right reason to introduce it. All we really want to do here is decouple the Audio itself, the control of that Audio, and the display of the current state of the Audio. We can express all of that in React through components.&lt;/p&gt;

&lt;p&gt;When &lt;a href="https://en.wikipedia.org/wiki/Inversion_of_control"&gt;inverting control&lt;/a&gt; of the Audio, this takes the form of putting the shared &lt;code&gt;Audio&lt;/code&gt; component toward the top of the tree rather than the bottom, where each Bleat had its own Audio instance. We could pass the instance down through &lt;a href="https://reactjs.org/docs/components-and-props.html"&gt;props&lt;/a&gt; but that could end up really messy, because the components which use it might be in arbitary positions in the component tree. The solution here is &lt;a href="https://reactjs.org/docs/context.html"&gt;context&lt;/a&gt;, the same mechanism react-redux uses to share Redux’s store down to &lt;code&gt;connect&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Visualizing the options
&lt;/h2&gt;

&lt;p&gt;We can use a simplified component hierarchy to show the differences between the context and props approaches.&lt;/p&gt;

&lt;p&gt;Before our changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Playlist&amp;gt;
  &amp;lt;Bleat&amp;gt;
    &amp;lt;Audio /&amp;gt;
  &amp;lt;/Bleat&amp;gt;
  &amp;lt;Bleat&amp;gt;
    &amp;lt;Audio /&amp;gt;
  &amp;lt;/Bleat&amp;gt;
&amp;lt;/Playlist&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using props to invert control of the audio:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Audio&amp;gt;
  &amp;lt;Playlist audio={audio}&amp;gt;
    &amp;lt;Bleat audio={audio} /&amp;gt;
    &amp;lt;Bleat audio={audio} /&amp;gt;
  &amp;lt;/Playlist&amp;gt;
&amp;lt;/Audio&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using context to pass the audio down without threading props:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;AudioProvider&amp;gt;
  &amp;lt;Playlist&amp;gt;
    &amp;lt;Bleat /&amp;gt;
    &amp;lt;Bleat /&amp;gt;
  &amp;lt;/Playlist&amp;gt;
&amp;lt;/Audio&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Sharing the audio
&lt;/h2&gt;

&lt;p&gt;We want to share an &lt;code&gt;Audio&lt;/code&gt; instance. We could also put it into a &lt;code&gt;ref&lt;/code&gt; to defer its instantiation to the component’s lifecycle, but this works for now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const audio = new Audio();

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We start by defining a &lt;code&gt;context&lt;/code&gt;, and initializing it with the Audio element we just created. We could also pass null &lt;code&gt;null&lt;/code&gt; so consumers of the context know if it’s been initialized or not, but we have it so we might as well use it. The &lt;code&gt;null&lt;/code&gt; pattern is also common if you are loading something asynchronously that you’ll provide later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const AudioContext = React.createContext(audio);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we want to define a provider component. Notice we’re exporting this - &lt;code&gt;AudioContext.Provider&lt;/code&gt; could just go straight into the app, but breaking it out allows us to package this nicely into a file without exporting the &lt;code&gt;AudioContext&lt;/code&gt; we created above.&lt;/p&gt;

&lt;p&gt;It also allows you do define more complex hooks in your &lt;code&gt;Provider&lt;/code&gt; and pass them down, but we don’t need that here. Passing &lt;code&gt;audio&lt;/code&gt; here could instead be a return value of &lt;code&gt;useRef&lt;/code&gt; or &lt;code&gt;useState&lt;/code&gt; rather than just the constant above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const AudioProvider = ({ children }) =&amp;gt; (
  &amp;lt;AudioContext.Provider value={audio}&amp;gt;{children}&amp;lt;/AudioContext.Provider&amp;gt;
);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now how do we get access to the &lt;code&gt;audio&lt;/code&gt; further down the tree? First we need to wrap any components that want to use it with the Provider we created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; (
  &amp;lt;AudioProvider&amp;gt;
    &amp;lt;Playlist /&amp;gt;
  &amp;lt;/AudioProvider&amp;gt;
);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can wrap the &lt;code&gt;useContext&lt;/code&gt; hook, again to avoid exporting the AudioContext:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const useAudio = React.useContext(AudioContext);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we can use our custom hook down the tree with &lt;code&gt;useAudio&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Playlist = () =&amp;gt; {
  const audio = useAudio();

  const play = React.useCallback((src) =&amp;gt; {
    audio.src = src;
    audio.play();
  }, [audio]);

  ...
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Making a playlist
&lt;/h2&gt;

&lt;p&gt;Now that we have a shared audio context, let’s make it do something. We’ll centralize control into the &lt;code&gt;Playlist&lt;/code&gt; component. It will hold info about all the clips to play (in a real application this would come from the server, but we’re just using a constant for now). Let’s add some state to the Playlist:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [isPlaying, setIsPlaying] = React.useState(false);
const [currentIndex, setCurrentIndex] = React.useState(null);
const [isPlayingAll, setIsPlayingAll] = React.useState(false);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When the clip ends, let’s move to the next one. We create this event listener inside a &lt;code&gt;useEffect&lt;/code&gt; to allow cleanup when the component unmounts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const maybeAdvancePlaylist = React.useCallback(() =&amp;gt; {
  const newIndex = currentIndex + 1;

  // stop at the last clip
  if (newIndex &amp;gt; BLEATS.length) {
    return;
  }

  // don't advance if a single clip was played
  if (isPlayingAll) {
    setCurrentIndex(newIndex);
    audio.src = BLEATS[newIndex].src;
    audio.play();
  }
}, [currentIndex, isPlayingAll]);

React.useEffect(() =&amp;gt; {
  audio.addEventListener("ended", maybeAdvancePlaylist);

  // when unmounting, clean up the event listener we've added
  return () =&amp;gt; {
    audio.removeEventListener("ended", maybeAdvancePlaylist);
  };
}, [audio, maybeAdvancePlaylist]);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Let’s see it!
&lt;/h2&gt;

&lt;p&gt;The following audio files should play through, using a shared audio context:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://campedersen.com/2020/04/21/react-audio/"&gt;View this post on campedersen.com for a working example&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hooks are fun
&lt;/h2&gt;

&lt;p&gt;The context + hooks approach has several advantages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;it doesn’t use any external libraries, but you could easily share &lt;a href="https://howlerjs.com/"&gt;Howler&lt;/a&gt; this way, for instance&lt;/li&gt;
&lt;li&gt;you can bring the &lt;code&gt;AudioContext&lt;/code&gt; file over to another project easily without altering its Redux system&lt;/li&gt;
&lt;li&gt;we can provide this functionality surgically, only touching the components we need, by not threading props&lt;/li&gt;
&lt;li&gt;the changes to the codebase are obvious - &lt;code&gt;useAudio&lt;/code&gt; can’t be much clearer in my opinion&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But the best part is that it’s &lt;strong&gt;fun&lt;/strong&gt;! Hooks are super simple to write, refactor, and abstract. And in my opinion frontend work is fun because you get to interact with it - audio adds on top of the visual part of that!&lt;/p&gt;

&lt;h3&gt;
  
  
  Thanks
&lt;/h3&gt;

&lt;p&gt;Thanks to &lt;a href="https://davekiss.com/"&gt;Dave&lt;/a&gt; and &lt;a href="https://dylanjpierce.com/"&gt;Dylan&lt;/a&gt; for helping proof read this post (my first in a couple years!) and to &lt;a href="https://ykyz.com"&gt;YKYZ&lt;/a&gt; for allowing me to write about this work.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
