简体   繁体   English

如何在React Router的多个私有页面上呈现这些组件?

[英]How can I render these components on multiple private pages in React Router?

Working with React Router 4, building out our web app. 使用React Router 4,构建我们的Web应用程序。

Here's the situation: I have five pages: "Home", "Schedule", "Profile", "Groups", and "Events". 情况如下:我有五个页面:“主页”,“时间表”,“个人资料”,“组”和“事件”。

Homepage can be accessed by anyone, the other four require login. 任何人都可以访问主页,其他四个需要登录。

What I'd like to do is render a navigation that will show up on those four pages, but NOT show up on the Home page. 我想做的是渲染一个导航,该导航将显示在这四个页面上,而不显示在主页上。

Here's what I've got in my router so far. 到目前为止,这是我在路由器中获得的。

const renderMergedProps = (component, ...rest) => {
  const finalProps = Object.assign({}, ...rest);
  return React.createElement(component, finalProps);
};

const PropsRoute = ({ component, ...rest }) => {
  return (
    <Route
      {...rest}
      render={routeProps => {
        return renderMergedProps(component, routeProps, rest);
      }}
    />
  );
};

const PrivateRoute = ({ component, isLoggedIn, ...rest }) => (
  <Route
    {...rest}
    render={routeProps => {
      //I'm currently forcing open the login with "true" just for testing

// This will just be (isLoggedIn ?) in the final. //这最终将只是(isLoggedIn?)。 return (true || isLoggedIn) ? 返回(true || isLoggedIn)吗? ( renderMergedProps(component, routeProps, rest) ) : ( ); (renderMergedProps(component,routeProps,rest)):(); }} /> ); }} />);

const RouterComponent = props => (
  <BrowserRouter>
    <div>
      <Route exact path="/" component={HomePage} />
      <Switch>
        <PrivateRoute
          isLoggedIn={props.isLoggedIn}
          path="/my-profile"
          component={MyProfile}
          title="My Profile"
        />
        <PrivateRoute
          isLoggedIn={props.isLoggedIn}
          path="/my-schedule"
          component={MySchedule}
          title="My Schedule"
        />
        <PrivateRoute
          isLoggedIn={props.isLoggedIn}
          path="/my-groups"
          component={MyGroups}
          title="My Groups"
        />
        <PrivateRoute
          isLoggedIn={props.isLoggedIn}
          path="/my-events"
          component={MyEvents}
          title="My Events"
        />
      </Switch>
    </div>
  </BrowserRouter>
);

const mapStateToProps = state => ({ 
  isLoggedIn: state.auth.status === "authorized"
});

export default connect(mapStateToProps)(RouterComponent);

You should be able to instead wrap your {Homepage} route inside the switch, and make sure that route is exact, and then have the PrivateRoutes just outside the switch but inside the Div. 您应该可以将{Homepage}路由包装在交换机内部,并确保该路由正确,然后将PrivateRoutes置于交换机外部但在Div内部。 Before your private routes but after the switch closes, have a route of "/" (not exact matching) that renders the Navbar. 在您的私人路线之前但在开关关闭之后,使用呈现为Navbar的路线“ /”(不完全匹配)。

By that logic, homepage will render exclusively on its own (it's inside a switch) at "/". 按照这种逻辑,主页将完全以自己的方式(位于开关内)呈现在“ /”位置。 If you hit the private routes, because you're outside a switch, they will render inclusively so it'll match on NavBar (not exact matching) and then match on the private route you're in. 如果您碰到了私人路线,因为您不在交换机之内,它们将包含所有内容,因此将在NavBar上进行匹配(不完全匹配),然后在您所在的私人路线上进行匹配。

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

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