hitchhiker部署

Welcome to the third part of the Hitchhiker’s Guide to React Router v4. In this article we’re going to focus on recursive paths. If you’ve missed the first two parts, you can find part 1 here and part 2 here.

欢迎阅读《 Hitchhiker React Router v4指南》的第三部分。 在本文中,我们将重点介绍递归路径。 如果你已经错过了前两个部分,你可以找到第1部分在这里和第2部分在这里 。

什么是递归路径? (What are recursive paths?)

Recursive paths are nothing more than paths that are composed of nested routes that render the same component to show nested views.

递归路径无非是由嵌套路径组成的路径,这些路径呈现相同的组件以显示嵌套视图。

Example: http://evedes.rockz/Topics/1/2/3/2/1

示例: http://evedes.rockz/Topics/1/2/3/2/1

It’s commonly used to do “breadcrumbs” in websites — a navigation pattern that shows where the user is in a site organic structure, a social network friends relationship tree, solve a lot of complex graph problems, analytics, or trace any kind of information that depends on the last path. This can be the case, for example, of a computer game where you go from one room to another and the path you took to get there needs to be tracked for some reason.

它通常用于在网站上做“面包屑” —一种导航模式,显示用户在站点有机结构中的位置,社交网络朋友关系树,解决许多复杂的图形问题,分析或跟踪任何形式的信息取决于最后一条路。 例如,在一个电脑游戏中,您可能会从一个房间转到另一个房间,而出于某种原因,您需要跟踪到达该房间的路径,就可能是这种情况。

Excited? Say “yeah”! ?

激动吗 说耶”! ?

So, let’s do some changes in our application to test this pattern applied in React Router v4.

因此,让我们在应用程序中进行一些更改以测试在React Router v4中应用的这种模式。

目标 (The Objective)

So, the idea here is to transform our Topic List.

因此,这里的想法是改变我们的主题列表。

Instead of having a list of Topics that are matched and that the user can navigate to and see each Topic Detail and get back (seen in Part I of this guide), let’s do a nested route that starts at Topic 1 and shows the user which Topics are related to it — by showing a list of Links which can be clicked to navigate to the next related Topic Detail. Each time you choose a topic, you can navigate to it, see it’s details, and see which topics are related to it.

让我们做一个嵌套的路由,从主题1开始并向用户显示,该主题没有匹配的主题列表,并且用户可以导航到该主题列表并查看每个主题详细信息并返回(参见本指南的第一部分 )。主题与此相关-通过显示链接列表,可以单击链接以导航到下一个相关的主题详细信息。 每次选择一个主题时,您都可以导航至该主题,查看其详细信息以及与之相关的主题。

routes.js (routes.js)

So in routes.js we’ve deleted the import of the TopicDetails component and corrected the routes to render the TopicList component when the path is /Topics/:topicId, besides the existing Route to /Topics.

因此,在routes.js我们已经删除组件TopicDetails的进口和纠正呈现题目列表组件的路由时的路径是/主题/:topicId,除了现有的布线 /主题

Both will render the same component with different match properties.

两者都会渲染具有不同匹配属性的相同组件。

TopicList.js (TopicList.js)

Besides the small correction above, I’ve heavily refactored the TopicList.js file. Let’s have a look at what we have there:

除了上面的细微修改外,我还大量重构了TopicList.js文件。 让我们来看看我们那里有什么:

We’ve imported Route and Link from the react-router-dom package because we’re going to invoke it later in the code.

我们已从react-router-dom包中导入RouteLink ,因为稍后将在代码中调用它。

We’ve created an array of objects which contains the list of topics. Each topic has a relatedTopics array that promotes the relationship among them.

我们创建了一个包含主题列表的对象数组。 每个主题都有一个relatedTopics数组,可促进它们之间的关系。

We’ve created a find function that receives the topic’s id as an argument and returns the item or topic that corresponds unequivocally to the id passed into it.

我们创建了一个find函数,该函数接收主题ID作为参数,并返回与传递给它的ID明确对应的项目或主题。

The parseInt(id, 10) usage makes sure that even if the argument passed into the find function is a string, it becomes an integer on the base 10 (decimal number system).

parseInt(id,10)的用法可确保即使传递给find函数的参数是一个字符串,它也将成为以10为底的整数(十进制系统)。

Observe that our topics id and relatedTopics values are primitive integers.

请注意,我们的主题idrelatedTopics值是原始整数。

To learn more about parseInt take a look HERE.

要了解有关parseInt的更多信息,请点击这里 。

The component TopicDetail starts by defining the variable topic. This will store the result of the function find which grabs the id that comes from the match object (router) when the component is invoked. It then returns the topic object that corresponds to that id.

组件TopicDetail首先定义变量topic 。 这将存储函数查找的结果,该结果将在调用组件时获取来自匹配对象(路由器)的ID 。 然后返回主题对象 对应于该id

With that topic object stored, it returns the Details of the topic and creates a dynamic unordered list with the related topics id and name.

存储该主题对象后,它将返回该主题的详细信息 ,并创建一个具有相关主题idname的动态无序列表。

Let’s see this in the browser:

让我们在浏览器中看到这一点:

Nice! It’s working!

真好! 工作正常!

So, when you click one of the links shown, it routes you to the next topic id:

因此,当您单击显示的链接之一时,它将带您到下一个主题id

Wow! This route is outside of the routes.js file! This is new. Observe that technically you can create routes inside any component.

哇! 该路由在routes.js文件之外! 这是新的。 从技术上讲,您可以在任何组件内创建路由。

Do not forget that isExact is false because the url doesn’t entirely match the path from the /Topics/:topicId as previously defined in the routes.js component.

不要忘记isExact为false,因为url/ routes /:topicId中的路径并不完全匹配route.js组件中先前定义的路径

In the end, we define and export the TopicList component which invokes TopicDetail with the match object above. But, as in each instance of TopicDetails when you’re triggering a Route, TopicDetail gets re-rendered with new match properties supplied by the Router at each instance.

最后,我们定义并导出它调用TopicDetail与匹配对象上方的题目列表组件。 但是,就像在触发路由时在TopicDetails的每个实例中一样TopicDetail将使用每个实例中路由器提供的新匹配属性重新呈现。

So now we are done! ?

现在我们完成了! ?

最后但并非最不重要的 (Last but not least)

I think that by this time you already have a good idea on how to start implementing recursive routes.

我认为到这时您已经对如何开始实施递归路由有了一个好主意。

I’ve chosen this example because it’s easy to understand and very useful for some basic stuff.

之所以选择这个示例,是因为它易于理解,并且对某些基本知识非常有用。

The changes I’ve made to the application, to produce this article, can be found in my GitHub repo.

我对应用程序所做的更改(以生成本文)可以在我的GitHub repo中找到。

参考书目 (Bibliography)

To make this article, I’ve used the React Router documentation that you can find here.

为了撰写本文,我使用了React Router文档,您可以在这里找到。

All the other sites I’ve used are linked along the document to add info or provide context to what I’ve tried to explain to you.

我使用过的所有其他网站都与文档链接在一起,以添加信息或提供与我尝试向您解释的内容的上下文。

This article is part 3 of a series called “Hitchhiker’s Guide to React Router v4”

本文是名为“ Hitchhiker的React Router v4指南”系列的第3部分。

  • Part I: Grok React Router in 20min

    第一部分:20分钟内的Grok React Router

  • Part II: [match, location, history] — your best friends!

    第二部分:[比赛,位置,历史]-您最好的朋友!

  • Part IV: route config, the hidden value of defining a route configuration array

    第四部分:路由配置,定义路由配置数组的隐藏值

? Thank you very much!

? 非常感谢你!

翻译自: https://www.freecodecamp.org/news/hitchhikers-guide-to-react-router-v4-21c99a878bf8/

hitchhiker部署

hitchhiker部署_Hitchhiker的React Router v4指南:无限远的递归路径!相关推荐

  1. hitchhiker部署_Hitchhiker的React Router v4指南:20分钟内完成Grok React Router

    hitchhiker部署 Hi fellow React Hitchhiker! Want a ride into React Router? Jump in. Let's go! 大家好,React ...

  2. hitchhiker部署_Hitchhiker的React Router v4指南:[比赛,位置,历史] –您最好的朋友!...

    hitchhiker部署 嘿! 欢迎来到< React Router v4旅行者指南>,第二部分! (Hey! Welcome to the Hitchhiker's Guide to R ...

  3. hitchhiker部署_《 Hitchhiker的React Router v4指南》:路由配置的隐藏值

    hitchhiker部署 Welcome to the Hitchhiker's Guide to React Router v4, Part IV! 欢迎来到< React Router v4 ...

  4. 关于React Router v4的虚张声势指南

    In this article, we'll cover the important things you can quickly learn to be informed and confident ...

  5. [React Router v4] Intercept Route Changes

    If a user has entered some input, or the current Route is in a "dirty" state and we want t ...

  6. [React Router v4] Conditionally Render a Route with the Switch Component

    We often want to render a Route conditionally within our application. In React Router v4, the Route ...

  7. 使用React Router v4的嵌套路由

    React Router v4 introduced a new declarative, component based approach to routing. With that approac ...

  8. React Router入门指南

    by Nader Dabit 纳德·达比特(Nader Dabit) React Router入门指南 (Beginner's Guide to React Router) Or what I wis ...

  9. react router v4 简介

    最近使用react router 遇到一个问题:当对某个路由添加参数的时候/list/:app 这列路由,点击这个路由以后再点击其他路由,location地址就追加到后面,问不是replace. /l ...

最新文章

  1. express web的一款mvc框架
  2. vs2015+opencv3.4安装及问题整理
  3. win10雷电3接口驱动_“雷电3”接口知识大科普
  4. iTerm2、Oh My Zsh、主题等
  5. Android 推断当前Activity是不是最后一个Activity 以及 应用或Activity是否存在
  6. Tomcat帮助文档翻译 未完成
  7. react中的render-props模式
  8. php和python-PHP和Python性能比较:放弃PHP改用Python
  9. 【bzoj 1102】[POI2007]山峰和山谷Grz(BFS)
  10. poj 2182 Lost Cows 线段树!!!!
  11. 传入一个月份获取该月的统计信息
  12. [网络应用]Foobar2000界面入门:认识Foobar的UI系统,熟悉三种主流界面插件
  13. xmpp 即时通讯
  14. mysql 分页 conut优化_mysql count函数与分页功能极限优化
  15. 微信小程序对接微信支付详细教程
  16. 硬件:Intel CPU发展史
  17. Gym 10102B 贪心
  18. 测试团队的建设和管理
  19. 天行健,君子以自强不息;地势坤,君子以厚德载物 释意
  20. Machine Learning Practical 爱宝课程记录week1

热门文章

  1. JackJson 使用记录
  2. 学习API网关遇到的名词
  3. 数据结构之【线性表】(顺序表、链表的基本操作实现)
  4. 数据库表(字段类型、约束、截断表、修改表字段、重命名表)
  5. idea 批量修改同一列_学会这个,1秒就可以批量处理文件
  6. ScratchCardView:刮刮卡视图组件
  7. Docker学习笔记_安装ActiveMQ
  8. BZOJ 2004 [Hnoi2010]Bus 公交线路
  9. android 52 粘滞广播
  10. 深入Java虚拟机之虚拟机体系结构