Table of Contents
1. Three common routes
2. React-Router Routing introduction
3. Use of React-Router
Home Web Front-end Front-end Q&A What are react routes

What are react routes

Jan 03, 2023 pm 02:53 PM
react routing

React routing includes: 1. Page routing, code such as "window.location.href='...'history.back()"; 2. h5 routing, code such as "window.onchange = function ( ) {console.log(window.location.hash)}"; 3. Hash routing, code such as "history.pushState(...)".

What are react routes

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

What are the react routes?

Routing in React

1. Three common routes

(1) Page routing

window.location.href='https://www.hao123.com/'
history.back()
Copy after login

(2) h5 routing

window.location = '#hash'
window.onchange = function () {
    console.log(window.location.hash)
}
Copy after login

(3) hash routing

//推进一个状态
history.pushState('name','title','/path')
//替换一个状态
history.replaceState('name','title','/path')
Copy after login

2. React-Router Routing introduction

1. Routing method

:h5 routing

:hash routing

2. Routing rules

: Package component

3. Reason option

: Solve the matching problem, match in order, if it matches to the front The ones behind them don't match.

For example: /path and path/list, path/list will match /path in order, which cannot achieve the desired effect. Add the exact keyword to achieve a complete match, which must be exactly the same. to match.

4. Jump navigation, equivalent to a tag

: Jump page

: Enhanced version of Link

5. Automatic jump

: No need to click, it will automatically jump when the execution reaches this block

3. Use of React-Router

1. Import package

yarn add react-router-dom

import {BrowserRouter, Route, Link} from "react-router-dom";

2. BrowserRouter package

<browserrouter>
    <div>
        <route></route>
        <route></route>
    </div>
</browserrouter>
Copy after login
  • component is used to modify the routing page to be jumped. The page content is: Home and Detail are two real components
  • path: Matching routing rules
  • exact: Modify the matching effect, adding it means exact matching, not adding it means fuzzy matching, for example:
  • path={'/'} means matching: http:/ /localhost:3000/, if it is http://localhost:3000/99, it will not match
  • path={'/detail/'} means matching: http://localhost:3000/detail/xxxxxxx , the latter is not limited, it will be OK if the previous match is successful

3. Passing value of route

http://localhost:3000/detail

(1) Routing parameter value passing

Routing parameters

Jump parameters:

Receive parameters:

this.props.match.params.id

Print result

3. Directly use

Access results

http://localhost:3000/detail/3

(2) Routing parameter value passing


Routing parameters

Jump parameters:

Receive parameters:

this.props.location.search

Print results

?id=3. Need to parse by yourself

Access result

http://localhost:3000/detail?id=3

4. Jump principle

The to parameter of Link matches the path parameter in Route, then the jump is executed and jumps to the component set in the component in Route.

A picture from the online summary describes it well:

What are react routes

Recommended learning: "react video tutorial"

The above is the detailed content of What are react routes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Java Apache Camel: Building a flexible and efficient service-oriented architecture Java Apache Camel: Building a flexible and efficient service-oriented architecture Feb 19, 2024 pm 04:12 PM

Apache Camel is an Enterprise Service Bus (ESB)-based integration framework that can easily integrate disparate applications, services, and data sources to automate complex business processes. ApacheCamel uses route-based configuration to easily define and manage integration processes. Key features of ApacheCamel include: Flexibility: ApacheCamel can be easily integrated with a variety of applications, services, and data sources. It supports multiple protocols, including HTTP, JMS, SOAP, FTP, etc. Efficiency: ApacheCamel is very efficient, it can handle a large number of messages. It uses an asynchronous messaging mechanism, which improves performance. Expandable

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

How to use Golang functions to handle web request routing How to use Golang functions to handle web request routing May 02, 2024 am 10:18 AM

In Golang, using functions to handle web request routing is an extensible and modular method of building APIs. It involves the following steps: Install the HTTP router library. Create a router. Define path patterns and handler functions for routes. Write handler functions to handle requests and return responses. Run the router using an HTTP server. This process allows for a modular approach when handling incoming requests, improving reusability, maintainability, and testability.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React and the Frontend: Building Interactive Experiences React and the Frontend: Building Interactive Experiences Apr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React vs. Vue: Which Framework Does Netflix Use? React vs. Vue: Which Framework Does Netflix Use? Apr 14, 2025 am 12:19 AM

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

See all articles