


Detailed explanation of how to use react-router to implement front-end routing
This article mainly introduces the detailed explanation of front-end routing and the usage posture of react-router, and introduces the usage of react-router in detail. If you are interested, you can learn more
routing
For those who have experience in SPA development, the term routing is not unfamiliar. The implementation techniques of front-end routing and back-end routing are different, but the principles are the same. Before the emergence of HTML5's history API, front-end routing was implemented through hash, and hash was compatible with lower version browsers. It needs to have # in its URI rules. The web service does not parse the hash, which means that the content after # will be automatically ignored by the web service. However, JavaScript can be read through window.location.hash. After reading the path and parsing it, it can respond to the logic of different paths. deal with.
A brief introduction to AngularJs UI-Router routing
If you have AngularJS development experience, # is not unfamiliar, angularjs has its own official implementation Routing system, there is also a relatively representative third-party nested routing mechanism UI-Router; as shown in the following code block:
.state("main.list",angularAMD.route({ url : '/list/:params',//url &参数 views : { "header@main" : angularAMD.route({ templateUrl : 'simple/view/main/html/main/Header.html', controller:'HeadController', controllerUrl:[ ****.js ] }), "menu@main" : angularAMD.route({ templateUrl : 'simple/view/main/html/main/MenuModule.html', controller : "MenuController", controllerUrl:[ ****.js] }), "mainContent@main":angularAMD.route({ templateUrl : 'simple/view/main/html/main/MainContent.html' }) } }))
The first parameter of the state() function It is routing. "main.list" is a nested routing mechanism. When the page jumps to the "main.list" route, the modules and their resources (html, js, etc.) under state ("main",*) will be loaded first. ), and then load the modules and resources (html, js, etc.) under the state ("main.list") to implement routing nesting;
react-router
-First a piece of code
<Router history={ hashHistory }> <Route path='/' component={CoreLayout}> <IndexRoute component={HomeView}/> <Route path=”/HODE_ROUTE/:param“ component={HomeView}/> <Route path=“ /AUDIT_ROUTE/:param" component={AuditView}/> <Route path=“/CHART_ROUTE” component={ChartView}/> </Route> </Router>
React-router implements router nesting in the form of jsx syntax similar to the DOM structure; with AngularJs UI-Router They seem to be very different, but in fact they have similar ideas;
Angular’s implementation logic:
Jump=》state=》module=》static resources (js, css, html) =》show (page display)
react-router implementation logic:
Jump=》path=》component=》static resources (js, css , html) =》show (page display)
This article mainly talks about react-router. The following is a brief introduction to the usage posture of react-router:
Getting started with common components of react-router
Nested routing
<Router history={hashHistory}> <Route path="/" component={App}> <Route path="/repos" component={Repos}/> <Route path="/about" component={About}/> </Route> </Router>
In the above code, when the user accesses /repos, he will first Load the App component, and then load the Repos component inside it.
<App> <Repos/> </App>
The sub-route does not need to be written in the Router component, and the routes attribute of the Router component can be passed in separately
let routes = <Route path="/" component={App}> <Route path="/repos" component={Repos}/> <Route path="/about" component={About}/> </Route>; <Router routes={routes} history={browserHistory}/>
path attribute
The path attribute of the Route component specifies the matching rules of the route. See the example below
<Route path="inbox" component={Inbox}> <Route path="messages/:id" component={Message} /> </Route>
In the above code, when the user accesses /inbox/messages/:id, the following components will be loaded.
<Inbox> <Message/> </Inbox>
IndexRoute component
Similar to index.html, the component is loaded by default. The following code loads the home component by default
<Router> <Route path="/" component={App}> <IndexRoute component={Home}/> <Route path="accounts" component={Accounts}/> <Route path="statements" component={Statements}/> </Route> </Router>
Now, when the user accesses /, the loaded component structure is as follows.
<App> <Home/> </App>
Redirect component
The Redirect component is used for routing jumps, that is, when users access a route, they will automatically jump to Another route.
<Route path="inbox" component={Inbox}> {/* 从 /inbox/messages/:id 跳转到 /messages/:id */} <Redirect from="messages/:id" to="/messages/:id" /> </Route>
Now access /inbox/messages/5, it will automatically jump to /messages/5.
Link
The Link component is used to replace the a tag and generate a link, allowing users to jump to another route after clicking. It's basically a React version of the a tag that receives the status of the Router.
Form processing
The Link component is used for normal user click jumps, but sometimes operations such as form jumps and button click jumps are also required. How to connect these situations with React Router?
The first method is to use browserHistory.push
##
handleSubmit(event) { event.preventDefault() const userName = event.target.elements[0].value const repo = event.target.elements[1].value const path = `/repos/${userName}/${repo}` browserHistory.push(path) }
handleSubmit(event) { // ... this.context.router.push(path) },
The second method is to use the context object.
export default React.createClass({ // ask for `router` from context contextTypes: { router: React.PropTypes.object }, handleSubmit(event) { // ... this.context.router.push(path) }, })
Multi-person collaborative development router file management
Generally, a project will have one The unified router file is equivalent to a router pool. Different requests request the matching path in the router pool and load the page required for the request. but. . . Every developer developing a component will need to configure routing, which will make the router file difficult to manage and may easily lead to conflicts or even failures. So. . , maybe, for a XXX.router file in each component folder, a hook function can be triggered when the front-end code is packaged and uploaded online, and the XXX.router file can be unified into the central router file, thereby avoiding multi-person operations. router file.The above is the detailed content of Detailed explanation of how to use react-router to implement front-end routing. 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

AI Hentai Generator
Generate AI Hentai for free.

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



The DirectX repair tool is a professional system tool. Its main function is to detect the DirectX status of the current system. If an abnormality is found, it can be repaired directly. There may be many users who don’t know how to use the DirectX repair tool. Let’s take a look at the detailed tutorial below. 1. Use repair tool software to perform repair detection. 2. If it prompts that there is an abnormal problem in the C++ component after the repair is completed, please click the Cancel button, and then click the Tools menu bar. 3. Click the Options button, select the extension, and click the Start Extension button. 4. After the expansion is completed, re-detect and repair it. 5. If the problem is still not solved after the repair tool operation is completed, you can try to uninstall and reinstall the program that reported the error.

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Many friends still don’t know how to use Baidu Netdisk, so the editor will explain how to use Baidu Netdisk below. If you are in need, hurry up and take a look. I believe it will be helpful to everyone. Step 1: Log in directly after installing Baidu Netdisk (as shown in the picture); Step 2: Then select "My Sharing" and "Transfer List" according to the page prompts (as shown in the picture); Step 3: In "Friend Sharing", you can share pictures and files directly with friends (as shown in the picture); Step 4: Then select "Share" and then select computer files or network disk files (as shown in the picture); Fifth Step 1: Then you can find friends (as shown in the picture); Step 6: You can also find the functions you need in the "Function Treasure Box" (as shown in the picture). The above is the editor’s opinion

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The KMS Activation Tool is a software tool used to activate Microsoft Windows and Office products. KMS is the abbreviation of KeyManagementService, which is key management service. The KMS activation tool simulates the functions of the KMS server so that the computer can connect to the virtual KMS server to activate Windows and Office products. The KMS activation tool is small in size and powerful in function. It can be permanently activated with one click. It can activate any version of the window system and any version of Office software without being connected to the Internet. It is currently the most successful and frequently updated Windows activation tool. Today I will introduce it Let me introduce to you the kms activation work

Potplayer is a very powerful media player, but many friends still don’t know how to use potplayer. Today I will introduce how to use potplayer in detail, hoping to help everyone. 1. PotPlayer shortcut keys. The default common shortcut keys for PotPlayer player are as follows: (1) Play/pause: space (2) Volume: mouse wheel, up and down arrow keys (3) forward/backward: left and right arrow keys (4) bookmark: P- Add bookmarks, H-view bookmarks (5) full screen/restore: Enter (6) multiple speeds: C-accelerate, 7) Previous/next frame: D/

How to use the shortcut keys for merging cells In daily work, we often need to edit and format tables. Merging cells is a common operation that can merge multiple adjacent cells into one cell to improve the beauty of the table and the information display effect. In mainstream spreadsheet software such as Microsoft Excel and Google Sheets, the operation of merging cells is very simple and can be achieved through shortcut keys. The following will introduce the shortcut key usage for merging cells in these two software. exist
