简体   繁体   English

React Router:路由内的路由

[英]React Router: Routes within routes

I am building a React app that has a static marketing site and a dynamic app. 我正在构建一个具有静态营销网站和动态应用程序的React应用程序。 I am using a combination of React Router and hooks to separate the two and ensure proper routing throughout. 我正在使用React Router和Hook的组合来将两者分开,并确保整个过程中路由正确。

To begin with, I want users who are logged in to be taken directly to the app when they hit the root ("/") and to the static/marketing site when not logged in. The main marketing site home page has a nav bar that allows users to access other routes such as "/about", whereas the app has a separate nav bar for app navigation. 首先,我希望登录的用户点击根目录(“ /”)后直接进入应用程序,而未登录时直接进入静态/营销网站。主要的营销网站首页具有导航栏允许用户访问其他路线,例如“ / about”,而该应用程序则具有用于导航的单独导航栏。

The problem is, while the authentication based routing for the root route seems to be working, and I can navigate to other routes specified in my top-level file, the routes that are included within my static/marketing site are not accessible. 问题是,当根路由认证的路由似乎是工作,我可以导航到我的顶层文件中指定的其他路线,这包括我的静态/营销网站的路由不可访问。

Top-level/index.js 顶级/index.js

const routing = (
  <Provider store={store}>
    <Router>
      <NavWrapper />
      {/* <Switch> */}
      <Route exact path="/" component={AuthWrapper} />
      <Route path="/signup" component={SignUp} />
      <Route path="/login" component={Login} />
      {/* </Switch> */}
    </Router>
  </Provider>
)

ReactDOM.render(routing, document.getElementById('root'));

AuthWrapper.js (here useAuth() is a React hook that evaluates global Redux isAuthenticated state) AuthWrapper.js(此处useAuth()是一个React钩子,用于评估全局Redux isAuthenticated状态)

const AuthWrapper = (props) => {
  return useAuth() ? <App /> : <Website />
}

export default AuthWrapper;

website/index.js 网站/index.js

export const Website = () => {
  return (
    <>
      {/* <SiteNav /> */}
      <Switch>
        <Route exact path="/" component={LandingPage} />
        <Route path="/about" component={AboutPage} />
        <Route path="/how-it-works" component={HowItWorksPage} />
        <Route path="/plans" component={PlansPage} />
        <Route path="/press" component={PressPage} />
        <Route path="/faq" component={FAQPage} />
        <Route path="/legal" component={LegalPage} />
        <Route path="/for-dieticians" component={DietitiansPage} />
        <Route path="/for-trainers" component={PTsPage} />
      </Switch>
  </>
  )
};

In my app, when I enter localhost:3000/ (unauthenticated) I am correctly taken to my home/landing page. 在我的应用程序中,当我输入localhost:3000 /(未经身份验证)时,我会正确进入首页/着陆页。 However, if I try to use the navigation (or type in any subroutes) to access "/about" for example, I get a blank screen (other than the nav bar). 但是,例如,如果我尝试使用导航(或键入任何子路线)来访问“ / about”,则会出现空白屏幕(导航栏除外)。 In the React dev tools, the component isn't even rendering. 在React开发工具中,该组件甚至没有渲染。

I think this is the issue: 我认为这是问题所在:

<Route exact path="/" component={AuthWrapper} />

You want Authwrapper to be shown for about as well, right? 您还希望同时显示Authwrapper,对吗? But the above only matches "/" exactly. 但以上仅与“ /”完全匹配。

You should enable the switch statement, but move the AuthWrapper Route to the bottom and then let it have this path: "/:rest*" (maybe "/*" works too, haven't used react-router in a while). 您应该启用switch语句,但是将AuthWrapper Route移到底部,然后使其具有以下路径:“ /:rest *”(也许“ / *”也可以使用,有一段时间没有使用react-router了)。

That way, it will use the Route if the first 2 don't match. 这样,如果前两个不匹配,它将使用Route。

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

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