我正在阅读React Router 中的静态路由与动态路由,我正在努力确定后者的优势(以及为什么 v4 选择使用它)。我可以看到列出应用程序(静态)的所有路由以及每个路由映射到的组件的优势,允许您跟踪给定特定 URL 将呈现的内容。但我没有看到动态路由的任何明显优势。
如果有的话,我只能看到缺点,因为没有明确的方法来查看 URL 将映射到什么状态,而无需从根应用程序元素开始并通过路由(尽管我可能会误会)。
动态路由解决什么情况?为什么它比静态路由更可取(可能特别是在 React 应用程序中)?
我正在阅读React Router 中的静态路由与动态路由,我正在努力确定后者的优势(以及为什么 v4 选择使用它)。我可以看到列出应用程序(静态)的所有路由以及每个路由映射到的组件的优势,允许您跟踪给定特定 URL 将呈现的内容。但我没有看到动态路由的任何明显优势。
如果有的话,我只能看到缺点,因为没有明确的方法来查看 URL 将映射到什么状态,而无需从根应用程序元素开始并通过路由(尽管我可能会误会)。
动态路由解决什么情况?为什么它比静态路由更可取(可能特别是在 React 应用程序中)?
从react-router文档:
当我们说动态路由时,我们指的是在您的 应用程序呈现时发生的路由,而不是在正在运行的应用程序之外的配置或约定中。
早期版本react-router(v4 之前)曾经有静态路由。这导致了应用程序中的集中式路由,例如:
<Router>
<Route path='/' component={Main}>
<IndexRoute component={Home} />
<Route path='about' component={About} />
<Route onEnter={verifyUser} path='profile' component={Profile} />
...
</Route>
</Router>
然而,这并不完全是React的做事方式。React 专注于使用基于组件的逻辑进行组合。因此,与其将我们的 Route 想象成一个静态系统,我们可以将它们想象成一个组件,这就是 react-router v4 所带来的以及它背后的主要哲学。
因此,我们可以Route像使用任何 React 组件一样使用。这让我们可以Route在构建不同组件时添加组件。这样做的一个好处是我们可以将路由逻辑与需要它们的组件分离。
该About组件可以处理所有路由并根据 url(例如/about/job或/about/life等)有条件地呈现 UI 的部分。
另一件要注意的事情是,Route组件要么为匹配的路由渲染组件,要么为null. 例如,以下Route呈现About对一个路径成分/about和null(或没有),否则。
<Route path='about' component={About} />
这也类似于我们在 React 中使用条件渲染组件的方式:
route === '/about' ? <About /> : null
现在,如果我们需要在组件内部About为路由渲染一些其他组件,/about/job或者/about/life我们可以这样做:
const About = ({ match ) => (
<div>
...
<Route path={`${match.url}/job`} component={Job} />
<Route path={`${match.url}/life`} component={Life} />
</div>
)
就我个人而言,我还发现这种方法更适合我,以防我使用带有代码拆分的动态导入,因为我可以在我的任何组件中添加动态路由。例如,
import Loadable from 'react-loadable';
const Loading = () => (
<div />
);
const Job = Loadable({
loader: () => import('./Job'),
loading: Loading,
});
const Life = Loadable({
loader: () => import('./Life'),
loading: Loading,
});
...
render() {
return (
...
<Route path={`${match.url}/job`} component={Job} />
<Route path={`${match.url}/life`} component={Life} />
)
}
动态路由的另一个很好的用例是创建响应式路由,这在react router 文档和推荐阅读中有很好的解释。这是文档中的示例:
const App = () => (
<AppLayout>
<Route path="/invoices" component={Invoices}/>
</AppLayout>
)
const Invoices = () => (
<Layout>
{/* always show the nav */}
<InvoicesNav/>
<Media query={PRETTY_SMALL}>
{screenIsSmall => screenIsSmall
// small screen has no redirect
? <Switch>
<Route exact path="/invoices/dashboard" component={Dashboard}/>
<Route path="/invoices/:id" component={Invoice}/>
</Switch>
// large screen does!
: <Switch>
<Route exact path="/invoices/dashboard" component={Dashboard}/>
<Route path="/invoices/:id" component={Invoice}/>
<Redirect from="/invoices" to="/invoices/dashboard"/>
</Switch>
}
</Media>
</Layout>
)
总结文档,您会注意到Redirect使用动态路由添加到大屏幕尺寸变得多么简单和声明性。在这种情况下使用静态路由会非常麻烦,并且需要我们将所有路由放在一个地方。动态路由简化了这个问题,因为现在逻辑变得可组合(就像组件一样)。
有一些问题是动态路由不容易解决的。静态路由的一个优点是它允许在渲染之前检查和匹配路由。因此它被证明在服务器端尤其有用。react 路由器团队也在研究一个名为react-router-config的解决方案,引用自:
随着 React Router v4 的引入,不再有集中的路由配置。在某些用例中,了解应用程序的所有潜在路由很有value,例如:
- 在渲染下一个屏幕之前在服务器上或生命周期中加载数据
- 按名称链接到路由
- 静态分析
希望这能很好地总结动态路由和静态路由以及它们的用例:)
根据 React-Router 文档:
当我们说动态路由时,我们指的是在您的应用程序呈现时发生的路由,而不是在正在运行的应用程序之外的配置或约定中。这意味着几乎所有东西都是 React Router 中的一个组件。
它的解释很清楚,所有你的路由都没有在你的应用程序开始时初始化,
在React-router v3或更低版本中,它使用静态路由并且所有路由都将在顶层初始化,并且嵌套曾经像
<Router>
<Route path='/' component={App}>
<IndexRoute component={Dashboard} />
<Route path='users' component={Users}>
<IndexRoute component={Home}/>
<Route path="users/:id" component={User}/>
</Route>
</Route>
</Router>
通过这个 API 设置,react-router 重新实现了 React 的一部分(生命周期等),它只是与 React 推荐使用的组合逻辑不匹配。
使用动态路由可以预见以下优势
嵌套路由
带有动态路由的嵌套路由更像是
const App = () => (
<BrowserRouter>
{/* here's a div */}
<div>
{/* here's a Route */}
<Route path="/todos" component={Todos}/>
</div>
</BrowserRouter>
)
// when the url matches `/todos` this component renders
const Todos = ({ match }) => (
// here's a nested div
<div>
{/* here's a nested Route,
match.url helps us make a relative path */}
<Route
path={`${match.path}/:id`}
component={Todo}
/>
</div>
)
在上面的例子中,只有当 /todos 与 route-path 匹配时,才会挂载 Todo 组件,然后才/todos/:id定义Route 路径。
响应路由
React-router 文档对此有一个很好的用例。
考虑用户导航到/invoices。你的应用程序适应不同的屏幕尺寸,它们有一个狭窄的视口,所以你只向他们显示发票列表和发票链接dashboard。他们可以从那里更深入地导航。
但是在大屏幕上,导航在左侧,仪表板或特定发票显示在右侧。
因此/invoices对于大屏幕不是有效的路由,我们希望重定向到/invoices/dashboard. 这可能会发生,用户将他/她的手机从portait to a landscape mode. 这可以使用动态路由轻松完成
const Invoices = () => (
<Layout>
{/* always show the nav */}
<InvoicesNav/>
<Media query={PRETTY_SMALL}>
{screenIsSmall => screenIsSmall
// small screen has no redirect
? <Switch>
<Route exact path="/invoices/dashboard" component={Dashboard}/>
<Route path="/invoices/:id" component={Invoice}/>
</Switch>
// large screen does!
: <Switch>
<Route exact path="/invoices/dashboard" component={Dashboard}/>
<Route path="/invoices/:id" component={Invoice}/>
<Redirect from="/invoices" to="/invoices/dashboard"/>
</Switch>
}
</Media>
</Layout>
)
将动态路由与 React Router 一起使用,请考虑components,而不是静态路由。
代码拆分
网络的一大特色是我们不必让访问者下载整个应用程序才能使用它。您可以将代码拆分视为增量下载应用程序。This is made possible with Dynamic Routing.
它带来的好处是不需要一次下载所有代码,因此它使初始渲染更快。
这是一篇很好的文章,可以帮助您为您的应用程序设置代码拆分
编写可组合的经过身份验证的路由
使用动态路由还可以更轻松地编写 PrivateRoutes(一个进行身份验证的 HOC),它允许对用户进行身份验证并为他们提供对特定路由的访问权限,否则进行重定向。我打的这个电话很笼统
典型的私有路由看起来像
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);
并且可以用作
<PrivateRoute path="/protected" component={Protected} />