0

假设我有很多路线,我想将它们分成几组。我将如何在 React with Reach Router 中实现这一点?

我基本上想要完成的示例:

const Router = () => {
    return (
        <Router>
            <AnonymousRoutes />
            <SecureRoutes />
        </Router>
    );
};

const AnonymousRoutes = () => {
    return (
        <>
            <Page1 path="1" />
            <Page2 path="2" />
        </>
    );
};

const SecureRoutes = () => {
    return (
        <>
            <Page3 path="3" />
            <Page4 path="4" />
        </>
    );
};
4

1 回答 1

1

编辑:所以,我的回答是基于对您的问题陈述的误读。我以为我阅读了 react-router,而不是到达路由器。我道歉。

所以使用片段正是你可能应该做的。但是,React Reach 目前不支持 Fragment。一线希望,看起来很快就会!

https://github.com/reach/router/pull/289

如果我正确理解您的问题,我认为您正在寻找的是从 react-router切换。

switch 组件允许开发人员分割他们的路线并在路径上呈现特定的内容。

它可能看起来像这样:

import { Switch, Route } from 'react-router'

<Switch>
  <Route exact path="/" component={Home}/>
  <Route path="/about" component={About}/>
  <Route path="/:user" component={User}/>
  <Route component={NoMatch}/>
</Switch>

于 2019-08-14T14:41:11.527 回答