- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读有关 static vs dynamic routing 的内容在 React Router 中,我正在努力确定后者的优势(以及为什么 v4 选择使用它)。我可以看到列出应用程序(静态)的所有路由以及每个路由映射到的组件的优点,允许您跟踪给定特定 URL 将呈现的内容。但我没有看到动态路线有任何明显的优势。
如果有的话,我只能看到缺点,因为没有明确的方法来查看 URL 将映射到什么状态,而无需从根应用程序元素开始并通过路由进行工作(尽管我可能是错误的)。
动态路由适用于什么情况?为什么它比静态路由更好(也许特别是在 React 应用程序中)?
最佳答案
来自 react 路由器 docs :
When we say dynamic routing, we mean routing that takes place as your app is rendering, not in a configuration or convention outside of a running app.
早期版本的 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 专注于使用基于组件的逻辑进行组合。因此,我们可以将路由想象成组件,而不是将其想象为静态系统,这就是 React-router v4 所引入的内容及其背后的主要理念。
因此,我们可以像使用任何 React 组件一样使用 Route
。这让我们可以在构建不同的组件时添加 Route
组件。这样做的一个优点是我们可以将路由逻辑与需要它们的组件解耦。
About
组件可以处理所有路由,并根据 url 有条件地渲染 UI 部分(例如 /about/job
或 /about/life
等)。
另一件需要注意的事情是,Route
组件将渲染匹配路由的组件或 null
。例如,以下 Route
呈现路由 /about
的 About
组件,否则呈现 null
(或无)。
<Route path='about' component={About} />
这也类似于我们在 React 中条件渲染组件的方式:
route === '/about' ? <About /> : null
现在,如果我们需要在路由 /about/job
或 /about/life
的 About
组件内渲染一些其他组件,我们可以这样做:
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 docs 对此进行了很好的解释。以及推荐阅读。这是文档中的示例:
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>
)
总结 docs ,您会注意到使用动态路由将 Redirect
添加到大屏幕尺寸变得多么简单和声明性。在这种情况下使用静态路由会非常麻烦,并且需要我们将所有路由放在一个地方。动态路由简化了这个问题,因为现在逻辑变得可组合(如组件)。
有些问题无法通过动态路由轻松解决。优势static routing是它允许在渲染之前检查和匹配路线。因此,它被证明非常有用,尤其是在服务器端。 React Router 团队也在开发一个名为 react-router-config 的解决方案。 ,引用其中:
With the introduction of React Router v4, there is no longer a centralized route configuration. There are some use-cases where it is valuable to know about all the app's potential routes such as:
- Loading data on the server or in the lifecycle before rendering the next screen
- Linking to routes by name
- Static analysis
希望这对动态路由和静态路由及其用例提供了很好的总结:)
关于reactjs - React 中动态路由与静态路由的优点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48817305/
下面我有一段代码可以在我的大多数简单程序中运行.. . 我想知道它是好是坏……以及利弊。 . win32头文件:win32.h // example of a not realated code to
了解到我们可以使用双括号初始化在 java 中初始化集合。并对其进行了一些搜索,发现由于其性能问题,不建议使用它。 private static final Set VALID_CODES = new
在 Java 中使枚举类似于类,而不是像 C/C++ 中的常量集合,有哪些优点? 最佳答案 您可以免费在编译时检查有效值。使用 public static int OPTION_ONE = 0; pu
我目前正在研究ruleML,我想知道是否有人可以给我列出一些优缺点? 谢谢 最佳答案 RuleML是语义网现有本体语言和查询系统的概括,例如OWL-DL,围绕RDF的工具集以及基于一阶逻辑的类似于Pr
每次我写某种形式的东西 let scorePopulation f population = Array.map (fun i -> f i) population 我最后问自己,我是否会写得更好
我总是从SQL专家那里听到,在SELECT语句中使用'*'符号效率不高,最好列出所有字段名称。 但是,对于将新字段添加到表然后相应地更新所有存储过程的情况下,我个人觉得效率不高。 那么使用'*'的优缺
我想知道通过将业务逻辑和数据与 Web 表单分离来分层 Web 应用程序的长期优势(如果有)。 (即表单、业务逻辑、数据不在同一个文件中,但每个都在另一个文件夹中的自己的类中,或者与其他类似的类组合)
我目前正在从事一个项目,其中分支和合并从一开始就运行得不太好。为了改变这一点,我们一直在讨论多种不同的方法。我想每个人对于如何做这类事情都有自己的哲学,所以它似乎也在这里。 我们一直在讨论的一件事是按
是 StyleCop好用,有什么优缺点? 我还下载了 StyleCop,Resharper StyleCop。 我正在使用 resharper,它有自己的编码标准,而 StyleCop 也有自己的,我
我刚刚开始学习C#,并使用Tutorials Point来完成此操作。在数组部分,我一直看到声明为 string[] sarray = { "Hello", "From", "Tutorials",
使 Java 中的枚举类似于类,而不是像 C/C++ 中那样只是常量的集合有哪些优势? 最佳答案 您可以获得有效值的免费编译时检查。使用 public static int OPTION_ONE =
您会在什么情况下使用 volatile 关键字?更重要的是:该计划如何从中受益? 根据我已经阅读和了解的内容:volatile 应该用于被不同线程访问的变量,因为它们比非 volatile 变量读取起
至少有三种众所周知的创建并发应用程序的方法: 通过锁定(.NET、Java)实现多线程和内存同步。软件事务内存 (link text) 是另一种同步方法。 异步消息传递(Erlang)。 我想了解是否
在对JCR or RDBMS进行了一些研究并阅读了其他posts之后,我仍然不确定是否将JCR over JPA用于文档管理系统,该系统必须处理不同的文档类型,非常大的文件和并发访问很多来自许多用户。
X 来自 http://drupal.org/node/953016 的帖子 Drupal 7 AJAX 系统很棒,它对表单甚至链接都非常流畅。 我无法以理智的方式解决方法是从 javascript
我的 .htaccess 中有一堆规则(子域、文件夹、用户特定的文件夹等...) 我现在正在使用这个正则表达式: ([a-z0-9A-Z]) 我正在寻找一个特定的规则,我找到了多种构建它的方法,我想知
好吧,我想这很清楚我试图对此进行推理的方向。 这些天有很多关于不变性(constness)的优点的讨论。 Java 中的并发编程一书也对此进行了很多讨论。 然而,这一切只是我所读到的。我个人没有用函数
我正在开发一个 Android 应用程序,但我遇到了一个无法以某种方式做出决定的决定。我希望有人能够阐明常见的做法以及支持它们的任何理论。 问题是这样的: 我想要一个包含图像和文本(例如表格)的项目列
是否可以使用 ≠ 而不是 !=。我知道这是一个额外的 alt 代码,我从未在项目中使用过它,但我已经对其进行了测试并且可以正常工作。除了必须 Alt +8800 之外还有什么优点/缺点吗? 编辑:我不
Visual Studio (sqlmetal) 生成的 dbml 文件带有映射到数据库表的实体。在您看来,这些类适合用作领域模型类吗?还是应该避免它们,只将它们隔离到数据访问层? 谢谢 最佳答案 在
我是一名优秀的程序员,十分优秀!