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

hitchhiker部署

Welcome to the Hitchhiker’s Guide to React Router v4, Part IV!

歡迎來到《 React Router v4旅行者指南》,第四部分!

Now that we’ve learned about recursive routes, let’s get back to our initial boilerplate, to avoid mixing concerns, and learn how to create a route configuration array.

既然我們已經了解了遞歸路由,那么讓我們回到最初的樣板中,避免混淆的問題,并學習如何創建路由配置數組。

So, just to recap a bit what we did in the beginning, let’s take a look at our initial routes.js file:

因此,讓我們回顧一下開始時所做的事情,讓我們看一下我們最初的routes.js文件:

Our Routes component is returning a div with a NavBar and a Switch where we’ve defined all the routes of our App.

我們的Routes組件將返回一個帶有NavBarSwitchdiv ,在其中定義了應用程序的所有路由。

Our first step in this Part 4 will be to define a routes array.

在第4部分中,我們的第一步將是定義一個路由數組。

路線數組 (Routes Array)

I’ve taken a look at our routes and created this array that defines each route and sub-route we had in our application.

我看了看我們的路線,并創建了這個數組,該數組定義了我們在應用程序中擁有的每個路線和子路線。

Nice! Now what?!? ?

真好! 怎么辦?!? ?

重構舊的硬編碼路由 (Refactor the Old Hardcoded Routes)

Now let’s clean our hardcoded routes and the Switch!

現在,讓我們清理我們的硬編碼路由和Switch!

Yeah! Goodbye all those lines of code. What are we really doing?

是的 再見所有這些代碼行。 我們到底在做什么?

Well, we’re mapping over the map array using an ES6 (fat arrow) callback to return an abstract component called <MakeRouteWithSubRoutes />. We are passing into it a key (just for React indexing purposes) and we are spreading the route info also into it.

好吧,我們正在使用ES6(胖箭頭)回調在map數組上進行映射,以返回名為<MakeRouteWithSubRoutes />的抽象組件。 我們在其中傳遞了一個密鑰(僅用于React索引目的),并且還在其中傳播了路線信息。

<MakeRouteWitheSubRoutes />組件 (<MakeRouteWitheSubRoutes /> Component)

In the meantime, we need to create that component. I’ve decided to create it apart and import it into the routes.js file.

同時,我們需要創建該組件。 我決定分開創建它,并將其導入routes.js文件。

Okay, this <MakeRouteWithSubRoutes/> Component is picking up each route you pass into it and returning a React Router <Route/> Component.

好的,這個<MakeRouteWithSubRoutes />組件將拾取傳遞給它的每條路由,并返回一個React Router <Route />組件。

As props, we have the path and the render method, which will invoke the route.component you want to render (then passing into it the spread props and the sub-routes that it needs to know).

作為道具,我們具有路徑和render方法,它們將調用您要渲染的route.component (然后將傳播的道具和它需要知道的子路線傳遞到其中)。

This routes are coming from the route config array — got it? Nice! ?

該路由來自路由配置數組-知道嗎? 真好! ?

TopicList(子路由) (TopicList (Sub-Routing))

This being said, let’s take a loot at the TopicList component because it’s the one receiving sub-routes from the route config array:

話雖這么說,讓我們來看看TopicList組件,因為它是從路由配置數組中接收子路由的一個:

So, what’s happening here? Our TopicList now is importing the <MakeRouteWithSubRoutes/> component and reusing with routes it has received.

那么,這是怎么回事? 我們的題目列表現在導入<MakeRouteWithSubRoutes />成分和與它已經接收的路由重新使用。

It also does a routes.map over the sub-routes and repeats the process done in the routes.js file.

它還在子路由上執行一個route.map ,并重復在route.js文件中完成的過程。

Take a minute to understand it and play with it!

花一點時間來理解它并玩它!

越來越多的子路由 (More and More Sub-Routing)

As you can see, this works quite well. It’s abstracted, there’s separation of concerns. The <MakeRoutesWithSubRoutes/> is a quite easy to use stateless component or function which doesn’t care about the routing content. It just routes whatever you feed to it.

如您所見,這很好。 它是抽象的,關注點分離。 <MakeRoutesWithSubRoutes />是一個非常容易使用的無狀態組件或函數,不需要關心路由內容。 它只是路由您提供的任何內容。

What if we wanted to do more sub-routing?

如果我們想做更多的子路由怎么辦?

Easy peasy! Just keep growing or redesigning your routes configuration array!

十分簡單! 只要保持增長或重新設計您的路線配置陣列即可!

See? The routes of the /Topics/:topicId could simply be an array like its parent routes. But I’ve decided to do better and invoke a function that calls an API and returns a new array of routes (just imagine it fetches the API ?).

看到? / Topics /:topicId的路由可以像其父路由一樣簡單地是一個數組。 但是我決定做得更好,并調用一個函數,該函數調用API并返回新的路由數組(想像一下它獲取了API?)。

So how can we check that in the App?

那么我們如何在App中檢查呢?

Let’s put a console.log inside the TopicDetail component and check what it is receiving:

讓我們將console.log放入TopicDetail組件中,并檢查其接收的內容:

I’m invoking routes() in console.log because now this sub-route is a function! Remember? All good! ?

我正在console.log中調用route() ,因為現在此子路由是一個函數! 記得? 都好! ?

Yeah Ma! We’ve done it! We’re receiving the routes dynamically and propagating those into our sub-routes and components. This is so great!

是的,馬! 我們做到了! 我們正在動態接收路線,并將其傳播到我們的子路線和組件中。 太好了!

NoMatch和歧義路線 (NoMatch And Ambiguous Routes)

Wait! Where’s our NoMatch Component?

等待! 我們的NoMatch組件在哪里?

Okay, let’s introduce it into our route config array:

好的,讓我們將其介紹給我們的路由配置數組:

Observe that :WhereTheHeckIsThat is a variable because it has the colon before it.

觀察到:WhereTheHeckIs那是一個變量,因為它前面有冒號。

What should we expect?

我們應該期待什么?

Let’s see it in action:

讓我們來看看它的作用:

Wow! As a matter of fact it’s rendering the NoMatch but it’s also rendering the Home View. Why?

哇! 事實上,它正在渲染NoMatch,但同時也在渲染Home View 。 為什么?

Well, what’s happening is that in our initial boilerplate we had a <Switch /> that was picking up the first <Route /> that matches the path remember?

好吧,發生的事情是,在我們的初始樣板中,我們有一個<Switch /> ,它正在拾取與所記住的路徑相匹配的第一個<Route />

So now, as we do not have the switch, it can match more than one path at a time!

所以現在,由于我們沒有開關,因此它一次可以匹配多個路徑!

These are called Ambiguous Routes. Router matched the /Home and at the same time /:WhereTheHeckIsThat because it’s kind of a wildcard that accepts everything.

這些稱為歧義路線。 Router匹配了/ Home并同時匹配了/ :WhereTheHeckIsThat,因為它有點像通配符,可以接受所有內容。

How to we correct that?

我們該如何糾正?

Simple: grab <Switch /> back!

簡單:搶回<Switch />

As you can see above, now the /Home is rendered alone, because <Switch /> found it and returned it immediately.

正如您在上面看到的,現在/ Home是單獨呈現的,因為<Switch />找到了它并立即返回了它。

If you put some unknown path in the URL, it triggers the :/WhereTheHeckIsThat and renders the NoMatch component as a default.

如果在URL中放置一些未知路徑,它將觸發:/ WhereTheHeckIsThat并將NoMatch組件呈現為默認值。

Great job! Everything is working as we’d expected and now we have a powerful route array configuration which allows us to have a lot of flexibility.

很好! 一切都按預期工作,現在我們擁有功能強大的路由陣列配置 ,使我們擁有很大的靈活性。

This really is the hidden value of having an abstraction and defining a route configuration array!

這確實是具有抽象并定義路由配置數組的隱藏價值!

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

This is the end of the Hitchhiker’s Guide To React Router v4.0!

Hitchhiker的React Router v4.0指南到此結束!

There is still are some stuff to pay attention to, but I prefer to let you deep dive a little bit in the boilerplates we’ve built and look for what you need in the React router website.

仍然有一些東西要注意,但是我更喜歡讓您深入研究我們制作的樣板并在React Router 網站上尋找您需要的東西。

I had so much fun doing this Guide that I think I’m going to start writing more and more :)

我在執行本指南時非常開心,以至于我將開始越來越多地撰寫:)

It was good not only because I was able to teach you something but also because I’ve also learned a lot in this process.

這很好,不僅因為我能夠教你一些東西,而且因為我在此過程中也學到了很多東西。

GitHub回購 (GitHub Repo)

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

在本文的第4部分的GitHub存儲庫中 ,可以找到我對應用程序所做的更改(以生成本文)。

參考書目 (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 4 of a series called “Hitchhiker’s Guide to React Router v4”

本文是稱為“ Hitchhiker的React Router v4指南”系列的第4部分。

  • Part I: Grok React Router in 20minutes

    第一部分:20分鐘內的Grok React Router

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

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

  • Part III: recursive paths, to the infinity and beyond!

    第三部分:無窮遠的遞歸路徑!

? Thank you very much!

? 非常感謝你!

翻譯自: https://www.freecodecamp.org/news/hitchhikers-guide-to-react-router-v4-c98c39892399/

hitchhiker部署

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393343.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393343.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393343.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

亮劍:PHP,我的未來不是夢(11)

2. 再說說“必備能力”說道能力&#xff0c;很多應屆畢業生都愿意給自己的點評是“我的學習能力很強”或者是“具有良好的學習意識”等。這里存在著這樣一個誤區&#xff01;把企業當成了學校&#xff01;當成了試驗室&#xff01;把老板當成了老師&#xff01;作為用人單位&am…

leetcode457. 環形數組循環

給定一個含有正整數和負整數的環形數組 nums。 如果某個索引中的數 k 為正數&#xff0c;則向前移動 k 個索引。相反&#xff0c;如果是負數 (-k)&#xff0c;則向后移動 k 個索引。因為數組是環形的&#xff0c;所以可以假設最后一個元素的下一個元素是第一個元素&#xff0c;…

Jquery的ajax提交成功后刷新頁面

轉載于:https://www.cnblogs.com/huoxiansudi/p/6646855.html

程序員編程經驗_在沒有實際編程的情況下成為更好的程序員

程序員編程經驗In this talk, Ryan Johnson explains what was for him the invisible step to becoming a better developer.在演講中&#xff0c;瑞安約翰遜(Ryan Johnson)解釋了對他來說&#xff0c;成為更好的開發人員這一無形的步驟。 You can watch the full video on t…

粘貼復制

方法1: 方法二: 方法三: // 第三種 ios 設備和 android設備均正常,但是pc端沒有//定義函數window.Clipboard (function(window, document, navigator) { var textArea, copy; // 判斷是不是ios端 function isOS() { return navigator.userAgent.mat…

leetcode109. 有序鏈表轉換二叉搜索樹(遞歸)

給定一個單鏈表&#xff0c;其中的元素按升序排序&#xff0c;將其轉換為高度平衡的二叉搜索樹。本題中&#xff0c;一個高度平衡二叉樹是指一個二叉樹每個節點 的左右兩個子樹的高度差的絕對值不超過 1。示例:給定的有序鏈表&#xff1a; [-10, -3, 0, 5, 9],一個可能的答案是…

mxnet教程

官方教程&#xff0c;講的還行&#xff0c;我用自己的實例講解。自己如何設計網絡&#xff0c;自己的迭代器 1&#xff1a;引入module&#xff1a; import mxnet as mx import numpy as np import cv2 import matplotlib.pyplot as plt import logginglogger logging.getLogge…

web動畫_Web動畫簡介

web動畫by CodeDraken由CodeDraken Web動畫簡介 (An Introduction to Web Animations) In this introduction to web animations article, we will cover basic CSS animations using pseudo-classes, transitions, and transformations.在此Web動畫簡介中&#xff0c;我們將介…

java統計空間占用_JVM —— Java 對象占用空間大小計算

引用類型(reference type&#xff1a; Integer)在 32 位系統上每一個占用 4bytes(即32bit&#xff0c; 才干管理 2^324G 的內存), 在 64 位系統上每一個占用 8bytes(開啟壓縮為 4 bytes)。四. 對齊填充HotSpot 的對齊方式為 8 字節對齊。不足的須要 Padding 填充對齊&#xff0…

源于十年來的點滴積累——《變革中的思索》印行出版

源于歸國十年來的點滴積累, 集結成書的《變革中的思索》&#xff0c;日前由電子工業出版社刊印出版。 這本書共有五個章節&#xff0c;分別是解碼創新、中國智造、管理心得、我和微軟、心靈記憶——前三章偏重技術&#xff0c;更多理性的思考; 后兩章則工作生活中的所見所聞&am…

SpringBoot聲明式事務

目錄 事務的基本特征隔離級別傳播行為Transcation事務的基本特征&#xff08;ACID&#xff09; Atomic&#xff08;原子性&#xff09; 事務中包含的操作被看作一個整體的業務單元&#xff0c;這個業務單元中的操作要么全部成功&#xff0c;要么全部失敗&#xff0c;不會出現部…

leetcode1437. 是否所有 1 都至少相隔 k 個元素

給你一個由若干 0 和 1 組成的數組 nums 以及整數 k。如果所有 1 都至少相隔 k 個元素&#xff0c;則返回 True &#xff1b;否則&#xff0c;返回 False 。 示例 1&#xff1a; 輸入&#xff1a;nums [1,0,0,0,1,0,0,1], k 2 輸出&#xff1a;true 解釋&#xff1a;每個 1 …

數據結構教程網盤鏈接_數據結構101:鏈接列表

數據結構教程網盤鏈接by Kevin Turney凱文特尼(Kevin Turney) Like stacks and queues, Linked Lists are a form of a sequential collection. It does not have to be in order. A Linked list is made up of independent nodes that may contain any type of data. Each no…

多線程之間的通信(等待喚醒機制、Lock 及其它線程的方法)

一、多線程之間的通信。 就是多個線程在操作同一份數據&#xff0c; 但是操作的方法不同。     如&#xff1a; 對于同一個存儲塊&#xff0c;其中有兩個存儲位&#xff1a;name sex&#xff0c; 現有兩個線程&#xff0c;一個向其中存放數據&#xff0c;一個打印其中的數…

Linux iptables 配置詳解

一、配置一個filter表的防火墻 1. 查看本機關于 iptables 的設置情況 # iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) …

06 Nginx

1.檢查linux上是否通過yum安裝了nginx rpm -qi nginx2.解決安裝nginx所依賴包 yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel ope…

java編寫安卓程序代碼,安卓:從Android的Java源代碼code創建UML

i am looking for a program that can create automatically an Uml from my Java-Android source code.I have tested ArgoUml, but it does not support Android.Have any one a suggestion?Thanks!解決方案I can second what Tom Morris wrote in the comment above. Even …

leetcode1052. 愛生氣的書店老板(滑動窗口)

今天&#xff0c;書店老板有一家店打算試營業 customers.length 分鐘。每分鐘都有一些顧客&#xff08;customers[i]&#xff09;會進入書店&#xff0c;所有這些顧客都會在那一分鐘結束后離開。 在某些時候&#xff0c;書店老板會生氣。 如果書店老板在第 i 分鐘生氣&#xf…

amazon alexa_在Amazon Alexa上推出freeCodeCamp編碼瑣事測驗

amazon alexaNow you can learn coding concepts hands-free using an Amazon Echo.現在&#xff0c;您可以使用Amazon Echo免提學習編碼概念。 freeCodeCamp.org contributor David Jolliffe created a quiz game with questions on JavaScript, CSS, networking, and comput…

第一類第二類丟失更新

第一類丟失更新 A事務撤銷時&#xff0c;把已經提交的B事務的更新數據覆蓋了。這種錯誤可能造成很嚴重的問題&#xff0c;通過下面的賬戶取款轉賬就可以看出來&#xff1a; 時間 取款事務A 轉賬事務B T1 開始事務 T2 開始事務 T3 查詢賬戶余額為1000元 …