2

I have some nested routes:

<Routes>
  <Route path="components" element={<Components />}>
    <Route path="alert" element={<Alerts />} />
    <Route path="button" element={<Buttons />} />
    <Route path="chip" element={<Chips />} />
  </Route>
</Routes>

I have an <Outlet /> setup in <Components> and the nested routes work as expected.

/components/alert
/components/button
/components/chip

If someone visits /components, I would like to redirect them to /components/alert. Previously using v5 I would use a <Redirect to="/components/alert"> component. How can I achieve this using v6?

I tried using the new <Navigate> component with a wildcard path like this:

<Route path="components" element={<Components />}>
  ...
  <Route path="*" element={<Navigate to="alert" replace />} />
</Route>

But this didn't seem to work.

0

3 Answers 3

4

To replace from Components with Alerts you can use an index as

<Routes>
  <Route path="components" element={<Components />}>
    <Route index element={<Alerts />} />
    <Route path="alert" element={<Alerts />} />
    <Route path="button" element={<Buttons />} />
    <Route path="chip" element={<Chips />} />
  </Route>
</Routes>

This would provide the Alerts component when /components is visited.

To redirect from /components to /components/alerts whenever the former is visited you can use Navigate in that index as

<Routes>
  <Route path="components" element={<Components />}>
    <Route index element={<Navigate replace to="/components/alerts" />} />
    <Route path="alert" element={<Alerts />} />
    <Route path="button" element={<Buttons />} />
    <Route path="chip" element={<Chips />} />
  </Route>
</Routes>
Sign up to request clarification or add additional context in comments.

2 Comments

This works and I think using index is the way to go. I just needed to use <Route index element={<Navigate replace to="alert" />} /> for the redirect, as the components part of the path is inherited. Thanks for the help.
For me, I am using the latest 6.4.2 and the redirection only works if I remove the element in the parent route like below. <Route path="components"> <Route index element={<Navigate replace to="alerts" />} />
0

With the details that you have provided, I can probably only think of one thing: is there any route conflicts? I think the problem with this is probably there.

I have created something similar to your code in Codesandbox and it worked fine for me.

My code as a reference (Codesandbox link may be deleted/dead in the future):

import "./styles.css";
import {
  BrowserRouter,
  Routes,
  Route,
  Outlet,
  Navigate
} from "react-router-dom";

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="components" element={<Components />}>
          <Route path="alert" element={<Another />} />
          <Route path="*" element={<Navigate to="alert" replace />} />
        </Route>
      </Routes>
    </BrowserRouter>
  );
}

function Components() {
  return (
    <>
      <div className="App">
        <h1>Hello CodeSandbox</h1>
        <h2>Start editing to see some magic happen!</h2>
      </div>

      <Outlet />
    </>
  );
}

function Another() {
  return (
    <div>
      <p>Another Alert Component</p>
    </div>
  );
}

Here is the Codesandbox Link. I tried to navigate to /components/123 and it instantly redirects to /components/alert for me. Perhaps the problem is with your configuration and/or the reverse proxy (if you're testing this in production)?

1 Comment

Going to /components/123 does redirect me to /components/alert but I want to redirect /components when there is nothing following the path.
0

Try this approach.

Codesandbox demo

Navigate in the demo to /components. It will redirect you to /components/alert

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="components" element={<Components />}>
          <Route path="alert" element={<Another />} />
          <Route path="/components" element={<Navigate to="/components/alert" replace />} />
        </Route>
      </Routes>
    </BrowserRouter>
  );
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.