Detailed explanation of steps to use React-router v4
This time I will bring you a detailed explanation of the steps to use React-router v4. What are the precautions when using React-router v4? The following is a practical case, let’s take a look.
Perhaps the best way to learn react-router is to use react-router-dom v4 to write a multi-page react application. This react application will include login, registration, homepage, contact and other pages. But first let's take a look at the concept of react router v4 and how it differs from v3.
React router v4 vs v3
v4 is a rewrite of react router, so it has many differences from v3. The main ones are:
In react router v4, routing is no longer centralized. It becomes part of the application layout and UI.
The router used by the browser is in
react-router-dom
. Therefore, when using it in the browser, you only need to importreact-router-dom
.New concepts
BrowerRouter
andHashRouter
. They each serve different situations. See below for details.No longer use
{props.children}
to handle nested routes.v4 routing is no longer exclusive by default and will have multiple matches. v3 is exclusive by default, and only one match will be used.
react-router-dom
is used in react-router for browsers. react-router
is divided into the following parts:
react-router is a common part for browsers and native applications.
react-router-dom is for browsers.
react-router-native is for native applications.
React-router vs react-router-dom vs react-router-native
react-router
is the core part. react-router-dom
Provides customized components required for browser use. react-router-native
specifically provides the parts that need to be used in native mobile applications. Therefore, if you implement browser development in this example, you only need to install react-router-dom
.
Installation
As mentioned above, we use react to develop web applications, so we only need to install react-router-dom
.
1 |
|
Understand and use react-router
BrowserRouter
, which is the implementation of theRouter
interface. Make the page and browser history consistent. For example:window.location
.HashRouter
is the same as above, except that it uses the hash part of the url, such as:window.location.hash
.MemoryRouter
,##NativeRouter
, handle routing in react native.
Static<a href="http://www.php.cn/wiki/188.html" target="_blank">Router</a>, handles static routing, the same as v3.
and
can be used in the browser. If you are using a non-static site and want to handle various URLs, then you need to use
BrowserRouter. On the contrary, if your server only handles static URLs, use
HashRouter.
Route component.
Route component can use the following attributes:
- path attribute,
- component attribute, its value is a component. This component will be drawn after
path
is successfully matched.
- exact attribute, this attribute is used to indicate whether this route is an exclusive match.
- strict attribute, this attribute specifies that the path only matches paths ending with a slash.
还有其他的一些属性,可以用来代替component
属性。
render属性,一个返回React组件的方法。传说中的rencer-prop就是从这里来的。
children属性,返回一个React组件的方法。只不过这个总是会绘制,即使没有匹配的路径的时候。
多数的时候是用component
属性就可以满足。但是,某些情况下你不得不使用render
或children
属性。
match
location
history
如:
使用组件:
1 |
|
使用render
属性实现内联绘制:
1 |
|
来看哥更复杂的:
1 2 3 4 5 6 7 8 |
|
使用children
:
1 2 3 4 5 6 7 8 9 10 11 |
|
更多关于react-router v4如何匹配路径的内容,请移步这里。
URL / Path / Route的参数
通常情况下,我们都会在路径里添加参数。这样方便在不同的组件之间传递一些必要的数据。那么我们如何才能获取到这些传递的参数,并传递给组件中呢?我们只需要在路径的最后加上/:param
。如:
1 2 3 4 5 6 |
|
一旦有路径可以匹配成功,那么就会穿件一个拥有如下属性的对象,并传入绘制的组件里:
url: 匹配的url。
path:就是path。
isExact:如果
path
和当前的widnow.location
的path部分完全相同的话。params:在URL里包含的参数。
理解并使用Link
Link
是react router v4特有的一个组件。是用来代替上一版的anchor link。使用Link
可以在React应用的不同页面之间跳转。与unclor会重新加载整个页面不同,Link
只会重新加载页面里和当前url可以匹配的部分。
Link
组件需要用到to
属性,这个属性的值就是react router要跳转到的地址。如:
1 2 3 4 |
|
当被点击的时候,会跳转到路径:/
。
to
属性的值可以是一个字符串,也可以是一个location(pathname, hash, state和search)对象。比如:
1 2 3 4 5 6 |
|
Link
也可以使用replace
属性,如果点击的话,那么history里的当前领会被replace。
和
NavLink
是Link
的一个子类,在Link组件的基础上增加了绘制组件的样式,比如:
1 2 3 |
|
使用react router dom实现你的第一个demo
现在我们用react router dom来实现第一个demo。
首先,引入必要的组件。比如:Route
和BrowserRouter
。
1 |
|
接下来,我们创建一些组件和一些Html标签。同时我们用react router v4里的Link
和NavLink
组件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
然后我们来创建需要的组件:
1 2 3 4 5 6 |
|
最后,写App
组件。
1 2 3 4 5 6 |
|
如你所见,react router v4的组件还非常的易用的。
理解和使用非排他的路由
在上例中,我们在HomePage
组件的路由里使用了属性exact
。
1 |
|
这是因为v4中的路由默认都是非排他的,这一点和v3的实现思路截然不同。如果没有exact
属性,HomePage
组件和其他的组件就会同事绘制在页面上。
如,当用户点了登录连接以后,"/"
和"/login"
都满足匹配条件,对应的登录组件和Home组件就会同时出现在界面上。但是,这不是我们期待的结果,所以我们要给"/"
path加上exact
属性。
现在我们来看看非排他的路由有什么优点。假如我们有一个子菜单组件需要显示在profile页面出现的时候也出现。我们可以简单的修改BasicLayout
来实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
这样我们就会看到对应于"/me"
路径的组件都绘制出来了。这就是非排他路由的好处。
理解排他路由
排他路由是react router v3的默认实现。只有第一个匹配的路由对应的组件会被绘制。这一点也可以用react router v4的Switch
组件来实现。在Switch
组件中,只有第一个匹配的路由<Route>
或者<Redirect>
会被绘制:
1 2 3 4 5 6 7 |
|
浏览器历史
react router v4中,提供了一个history
对象。这个对象包含了多个api,可以用来操作浏览器历史等。
你也可以在React应用里使用history
对象的方法:
1 2 |
|
用另外的方法可以写成:
1 2 |
|
使用<Redirect>组件实现重定向
无论何时你要重定向到另外一个地址的时候,都可以使用Redirect
组件:
1 2 3 4 5 |
|
或者,更为简单的:
1 |
|
最后
react router v4让开发react应用变得更加的简单。让react应用内的页面跳转更加简单。你只需要声明一个BrowserRouter
或者HashRouter
,然后在它的内部放上一系列的Route
组件,这些主键只要包含path
和component
属性。无论何时有了匹配的路由,那么它就会进行非排他的绘制(所有匹配的路由都会绘制)。你也可以把Route
放在Switch
组件里来实现排他的绘制(只有第一个匹配的路由会被绘制)。你可以在路径中传递参数,match
对象会保留这些参数。最后,所有在web中使用的路由组件都包含在react-router-dom
中。只需要引入react-router-dom
就可以使用。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
Chart.js 轻量级HTML5图表绘制工具库使用步骤详解
The above is the detailed content of Detailed explanation of steps to use React-router v4. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Apple rolled out the iOS 17.4 update on Tuesday, bringing a slew of new features and fixes to iPhones. The update includes new emojis, and EU users will also be able to download them from other app stores. In addition, the update also strengthens the control of iPhone security and introduces more "Stolen Device Protection" setting options to provide users with more choices and protection. "iOS17.3 introduces the "Stolen Device Protection" function for the first time, adding extra security to users' sensitive information. When the user is away from home and other familiar places, this function requires the user to enter biometric information for the first time, and after one hour You must enter information again to access and change certain data, such as changing your Apple ID password or turning off stolen device protection.
