简体   繁体   English

go 如何在不渲染 React 中的某些组件的情况下进行特定路由?

[英]How to go specific route without rendering some components in React?

I am using react-router-dom v6.我正在使用react-router-dom v6。 I want my Login page to be rendered without the Sidebar and Topbar components.我希望在没有SidebarTopbar组件的情况下呈现我的Login页面。 How to do it?怎么做?

function App() {
  return (
    <Router>
      <Container>
        <Sidebar />
        <Content>
          <Topbar />
          <MainContent>
            <Routes>
              <Route path="/" element={<Home />} />
              <Route path="/users" element={<UserList />} />
              <Route path="/users/:id" element={<User />} />
              <Route path="/newUser" element={<NewUser />} />
              <Route path="/products" element={<ProductList />} />
              <Route path="/products/:id" element={<Product />} />
              <Route path="/newProduct" element={<NewProduct />} />
              <Route path="/login" element={<Login />} />
            </Routes>
          </MainContent>
        </Content>
      </Container>
    </Router>
  );
}

I don't want my login page to render inside the MainContent but taking the whole page without Sidebar and Topbar .我不希望我的登录页面呈现在MainContent内,而是在没有SidebarTopbar的情况下占用整个页面。

I tried moving the Routes upper and have the login route above the sidebar but there is an error Error: [Sidebar] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>我尝试将Routes上移并将登录route放在侧边栏上方,但出现错误Error: [Sidebar] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment> Error: [Sidebar] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>

Since SideBar and TopBar appear to be part of a "layout", and you want a different "layout" specifically for "/login" then I suggest abstracting the container components into layout components.由于SideBarTopBar似乎是“布局”的一部分,并且您想要专门针对“/ login”的不同“布局”,因此我建议将容器组件抽象为布局组件。 Each layout container should render an Outlet for their respective nested routes.每个布局容器都应该为它们各自的嵌套路由渲染一个Outlet

const AppLayout = () => (
  <Container>
    <Sidebar />
    <Content>
      <Topbar />
      <MainContent>
        <Outlet />
      </MainContent>
    </Content>
  </Container>
);

const LoginLayout = () => (
  <Container>
    <Content>
      <MainContent>
        <Outlet />
      </MainContent>
    </Content>
  </Container>
);

... ...

function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<AppLayout />}>
          <Route index element={<Home />} />
          <Route path="users" element={<UserList />} />
          <Route path="users/:id" element={<User />} />
          <Route path="newUser" element={<NewUser />} />
          <Route path="products" element={<ProductList />} />
          <Route path="products/:id" element={<Product />} />
          <Route path="newProduct" element={<NewProduct />} />
        </Route>
        <Route path="/login" element={<LoginLayout />}>
          <Route index element={<Login />} />
        </Route>
      </Routes>
    </Router>
  );
}
function App() {
    return (
        <Router>
            <Container>
                {
                    Login || <Sidebar />
                }

                <Content>
                    {
                        Login || <Topbar />
                    }
                    <MainContent>
                        <Routes>
                            <Route path="/" element={<Home />} />
                            <Route path="/users" element={<UserList />} />
                            <Route path="/users/:id" element={<User />} />
                            <Route path="/newUser" element={<NewUser />} />
                            <Route path="/products" element={<ProductList />} />
                            <Route path="/products/:id" element={<Product />} />
                            <Route path="/newProduct" element={<NewProduct />} />
                            <Route path="/login" element={<Login />} />
                        </Routes>
                    </MainContent>
                </Content>
            </Container>
        </Router>
    );
}

try conditional rendering.尝试条件渲染。 It works for me!这个对我有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM