search

Home  >  Q&A  >  body text

React Router nested routing and use of search parameters

When using search parameters in a nested React route, when I'm on /app, and after clicking the link that navigates to /app/user, the navigation doesn't work.

If I try to use without nesting, it works. But why it doesn't work when nested.

Codesandbox: CodeSandBox link

<Routes>
    <Route path="/" element={<LandingPage />} />
    <Route path="/app" element={<Main />}>
      <Route path=":user" element={<User />} />
    </Route>
    <Route path="*" element={<PageNotFound />} />
  </Routes>
P粉541796322P粉541796322230 days ago1576

reply all(1)I'll reply

  • P粉464113078

    P粉4641130782024-04-06 00:10:01

    try it

    <Routes>
            <Route path="/" element={<LandingPage />} />
            <Route path="/app" element={<Main />} />
            <Route path="/app/:user" element={<User />} />
            <Route path="*" element={<PageNotFound />} />
          </Routes>

    If you want to nest the User.js component, you need to add Outlet in Main.js

    import { Link, Outlet } from "react-router-dom";
    
    export default function Main() {
      return (
        <div>
          <p>Main Page</p>
          <Link to="/app/ashish">点击进入用户页面</Link>
    
          <Outlet/>
        </div>
      );
    }

    reply
    0
  • Cancelreply