title: React-Router V6 Display Previous Route
body: <p>I'm setting up a 404 page. It will live at /404 and I will be using <code>&lt;Navigate path='/404' /&gt;</code> for any unknown routes.  This all works fine, but I'd also like to display the URL details and even more so log the bad links. To do that, I need access to the previous route details.</p>
  <p>There is a similar article, but it discusses navigating history, not displaying the history information.</p>
  <p><a href="https://stackoverflow.com/questions/65948671/how-to-go-back-to-previous-route-in-react-router-dom-v6">How to go back to previous route in react-router-dom v6</a></p>
  <p>I've tried various combinations of the following, trying to grab details from useLocation and passing as state, but it throws the error: <code>Error: useLocation() may be used only in the context of a &lt;Router&gt; component.</code></p>
  <pre><code>&lt;BrowserRouter&gt;
    &lt;Routes&gt;
      &lt;Route path=&quot;/&quot; element={&lt;Homepage /&gt;} /&gt;
      &lt;Route path=&quot;/404&quot; element={&lt;NoPageFound /&gt;} /&gt;
      &lt;Route path=&quot;*&quot; element={&lt;Navigate to='/404' state={{ prevRoute: useLocation() }} /&gt;} /&gt;
    &lt;/Routes&gt;
  &lt;BrowserRouter&gt;
  </code></pre>
  <p>Is there some other way I can access the previous route details from within <code>&lt;NoPageFound /&gt;</code> for display/logging purposes?</p>