You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React DOM v6嵌套路由故障:无法加载profile/team-members嵌套组件

Fixing Nested Routes in React Router v6

Hey there! I see you're stuck getting your nested profile/team-members route to work properly—no worries, this is a common gotcha with React Router v6, and it's easy to fix.

The Problem

Right now, your nested route setup looks correct in the App component, but the Profile component isn't set up to display the child route content. In React Router v6, parent route components must include an <Outlet /> to act as a placeholder where child route elements get rendered. Without this, your TeamMembers component has nowhere to show up!

The Solution

  1. First, update your Profile component to import and render the Outlet from react-router-dom:
// Pages/Profile/Profile.jsx
import React from "react";
import { Outlet } from "react-router-dom"; // Add this import

const Profile = () => {
  return (
    <div>
      {/* Your existing Profile content goes here */}
      <h2>Profile Page</h2>
      <p>This is the main profile content.</p>

      {/* Add the Outlet here to render child routes */}
      <Outlet />
    </div>
  );
};

export default Profile;
  1. Keep your App component's route configuration as it is—you already have the nested structure right:
// App.jsx (your existing code is correct here)
import React from "react";
import {BrowserRouter, Routes, Route} from "react-router-dom";
import Home from "./Pages/Home/Home";
import Registration from "./Pages/Registration/Registration";
import Profile from "./Pages/Profile/Profile";
import TeamMembers from "./Pages/TeamMembers/TeamMembers";

const App = () => {
  return(
    <BrowserRouter>
      <Routes>
        <Route index element={<Home />} />
        <Route path="registration" element={<Registration />} />
        <Route path="profile" element={<Profile />} >
          <Route path="team-members" element={<TeamMembers />}/>
        </Route>
      </Routes>
    </BrowserRouter>
  )
}

export default App;

Why This Works

The <Outlet /> component tells React Router exactly where to inject the child route's component (in this case, TeamMembers) when you navigate to profile/team-members. When you visit just profile, the Outlet will be empty, so you'll only see the main Profile content. When you visit profile/team-members, the Outlet will render the TeamMembers component right where you placed it in the Profile component.

That's all you need to do—this should fix your routing issue!

内容的提问来源于stack exchange,提问作者Toma Tomov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 19:47:36