title: How to add a function which listens to the Audio's current playtime when the user leaves/refreshes the page?
body: <p>I have inserted an Audio into my React web page. I'm trying to record the current audio playtime of the audio when the user leaves the page (e.g. refresh/jump to a different page) so that the audio can restart from that point when the user revisits the page. My understanding is that I need to add an event listener, or use something like componentWillUnmount. Can anyone help me on this? Thank you.</p>
  <pre><code>// @flow
  import React, { Component } from &quot;react&quot;;
  
  export class Audio extends React.Component {
    constructor(props) {
      super(props);
      this.state = { time: 0 };
    }
  
    render() {
      return (
        &lt;audio
          loop
          autoPlay
          currentTime={this.state.time}
          className=&quot;backgroundAudio&quot;
          pause={this._onPause}
        &gt;
          &lt;source
            src=&quot;example.mp3&quot;
            type=&quot;audio/mpeg&quot;
          /&gt;
          Your browser does not support the audio element.
        &lt;/audio&gt;
      );
    }
  }
  </code></pre>