javascriptreact-routerreact-router-domurlsearchparams

How to use Navigate component to capture incoming query string / searchParams?


Currently I have

createBrowserRouter(
  createRoutesFromElements(...)
);

and nested within there is a:

<Route index element={<Navigate to="/raters/start/" />} />

This works fine for the base URL, except that we now need to capture any query strings on the original route and pass those on when we re-route the browser.

Is there something like this functionality available?

<Route
  index
  path="?:queryString"
  element={<Navigate to={`/raters/start?${queryString}`} />}
/>

Solution

  • The URL search string is not used for route path matching, it is invalid to include it in the path prop string. You'll need to manually capture and forward the URL path + searchParams.

    Examples: