React Router nested routing and use of search parameters
P粉541796322
P粉541796322 2024-04-05 13:48:33
0
1
1574

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粉541796322
P粉541796322

reply all(1)
P粉464113078

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>
  );
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template