title: Navigating back when user confirms in browser modal
body: <p>I am trying to alert the user of unsaved changes when they use the back button in the browser and it works when the user hits cancel on the confirm modal but when they click ok to confirm they want to go back the page does not go back.</p>
  <p>These are my funcitons for handling back:</p>
  <pre><code> const handleBrowserBackBtn = () =&gt; {
          handleBackClick();
          window.history.pushState(null, document.title, window.location.href);
      }
  
      useLayoutEffect(() =&gt; {
          window.history.pushState(null, document.title, window.location.href);
          window.addEventListener(&quot;popstate&quot;, handleBrowserBackBtn);
          return () =&gt; {
            window.removeEventListener(&quot;popstate&quot;, handleBrowserBackBtn);
          };
        });
  
      const handleBackClick = () =&gt; {
          if(addingNew || editing){
              if(confirm(&quot;You have unsaved changes. Are you sure you want to go back?&quot;)){
                  window.location.back()
              };
          }
      }
  </code></pre>
  <p>I am trying to send the user back to the home screen. But I get this endless loop of confirm pop up.</p>