


Detailed explanation of React routing management and use of React Router
This time I will bring you a detailed explanation of React routing management and the use of React Router. What are the precautions for React routing management and the use of React Router? Here are practical cases, let’s take a look.
What does React Router do? The official introduction is:
The following are some usages of React Router: A simple rendering RouteA complete routing library for React, keeps your UI in sync with the URL. It has a simple API with Powerful features like lazy code loading, dynamic route matching, and location transition handling built right in. Make the URL your first thought, not an after-thought. Synchronization, powerful features such as code lazy loading, dynamic route matching, path transition processing, etc. can be realized through a simple API.
There is one thing to keep in mind, Router As a React component, it can be rendered.
// ... import { Router, Route, hashHistory } from 'react-router' render(( <router> <route></route> </router> ), document.getElementById('app'))HashHistory is used here - it manages the routing history and the hash part of the URL. Add more routes and specify their corresponding components
import About from './modules/About' import Repos from './modules/Repos' render(( <router> <route></route> <route></route> <route></route> </router> ), document.getElementById('app'))二Link
// modules/App.js
import React from 'react'
import { Link } from 'react-router'
export default React.createClass({
render() {
return (
<p>
</p><h1 id="React-Router-Tutorial">React Router Tutorial</h1>
If we want to add a navigation bar, it needs to exist on every page. If there is no router, we need to encapsulate each nav component and reference and render it in each page component. As the application grows the code becomes redundant. React-router provides another way to nest shared UI components.
In fact, our app is a series of nested boxes, and the corresponding url can also illustrate this nested relationship:<app> {/* url / */} <repos> {/* url /repos */} <repo></repo> {/* url /repos/123 */} </repos> </app>Therefore, we can use the handle
component Nest
into the public component App so that the navigation bar Nav and other public parts on the App component can be shared:// index.js // ... render(( <router> <route> {/* 注意这里把两个子组件放在Route里嵌套在了App的Route里/} <route></route> <route></route> </route> </router> ), document.getElementById('app'))Next, render the children in the App:
// modules/App.js // ... render() { return ( <p> </p><h1 id="React-Router-Tutorial">React Router Tutorial</h1>
- About
- Repos
One of the differences between the Link component and the a tag is that Link can know whether the path it points to is a valid route.
// modules/NavLink.js import React from 'react' import { Link } from 'react-router' export default React.createClass({ render() { return <link> } })
// modules/App.js import NavLink from './NavLink' // ...
Consider the following url:
/repos/reactjs/react-router/ repos/facebook/react
They may correspond to this form:
The variable parameters in
url
can be obtained through this.props.params[paramsName]:// modules/Repo.js import React from 'react' export default React.createClass({ render() { return ( <p> {/* 注意这里通过this.props.params.repoName 获取到url中的repoName参数的值 */} </p><h2 id="this-props-params-repoName">{this.props.params.repoName}</h2> ) } })
// index.js // ... // import Repo import Repo from './modules/Repo' render(( <router> <route> <route></route> {/* 注意这里的路径 带了 :参数 */} <route></route> <route></route> </route> </router> ), document.getElementById('app'))Next visit /repos/reactjs/react-router and /repos/ facebook/react will see different content. 6 Default Route
// index.js
import { Router, Route, hashHistory, IndexRoute } from 'react-router'
// and the Home component
import Home from './modules/Home'
// ...
render((
<router>
<route>
{/* 注意这里* /}
<indexroute></indexroute>
<route>
<route></route>
</route>
<route></route>
</route>
</router>
), document.getElementById('app'))
IndexRoute is added here to specify the default path / corresponding component. Note that it has no path attribute value.
The previous example has always used hashHistory, because it can always run, but a better way is Using Browser History, it does not rely on hashed ports (#).
First you need to change index.js:// ... // bring in `browserHistory` instead of `hashHistory` import { Router, Route, browserHistory, IndexRoute } from 'react-router' render(( {/* 注意这里 */} <router> {/* ... */} </router> ), document.getElementById('app'))Secondly you need to modify the local service configuration of webpack, open package.json and add –history-api-fallback:
复制代码 代码如下:
"start": "webpack-dev-server --inline --content-base . --history-api-fallback"
最后需要在 index.html中 将文件的路径改为相对路径:
<!-- index.html --> <!-- index.css 改为 /index.css --> <link> <!-- bundle.js 改为 /bundle.js --> <script></script>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of React routing management and use of React Router. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.