I'm setting up a 404 page. It will live at /404 and I will be using <Navigate path='/404' />
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.
There is a similar article, but it discusses navigating history, not displaying the history information.
How to go back to previous route in react-router-dom v6
I've tried various combinations of the following, trying to grab details from useLocation and passing as state, but it throws the error: Error: useLocation() may be used only in the context of a <Router> component.
<BrowserRouter>
<Routes>
<Route path="/" element={<Homepage />} />
<Route path="/404" element={<NoPageFound />} />
<Route path="*" element={<Navigate to='/404' state={{ prevRoute: useLocation() }} />} />
</Routes>
<BrowserRouter>
Is there some other way I can access the previous route details from within <NoPageFound />
for display/logging purposes?