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)

React Router User Guide: How to implement front-end routing control React Router User Guide: How to implement front-end routing control Sep 29, 2023 pm 05:45 PM

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

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.

Use JavaScript functions to implement web page navigation and routing Use JavaScript functions to implement web page navigation and routing Nov 04, 2023 am 09:46 AM

In modern web applications, implementing web page navigation and routing is a very important part. Using JavaScript functions to implement this function can make our web applications more flexible, scalable and user-friendly. This article will introduce how to use JavaScript functions to implement web page navigation and routing, and provide specific code examples. Implementing web page navigation For a web application, web page navigation is the most frequently operated part by users. When a user clicks on the page

Implementation method and experience summary of flexibly configuring routing rules in PHP Implementation method and experience summary of flexibly configuring routing rules in PHP Oct 15, 2023 pm 03:43 PM

Implementation method and experience summary of flexible configuration of routing rules in PHP Introduction: In Web development, routing rules are a very important part, which determines the corresponding relationship between URL and specific PHP scripts. In the traditional development method, we usually configure various URL rules in the routing file, and then map the URL to the corresponding script path. However, as the complexity of the project increases and business requirements change, it will become very cumbersome and inflexible if each URL needs to be configured manually. So, how to implement in PHP

Dynamic addition and deletion methods of routes in uniapp Dynamic addition and deletion methods of routes in uniapp Dec 17, 2023 pm 02:55 PM

Uniapp is a cross-end framework based on Vue.js. It supports one-time writing and generates multi-end applications such as H5, mini programs, and APPs at the same time. It pays great attention to performance and development efficiency during the development process. In Uniapp, the dynamic addition and deletion of routes is a problem that is often encountered during the development process. Therefore, this article will introduce the dynamic addition and deletion of routes in Uniapp and provide specific code examples. 1. Dynamic addition of routes Dynamic addition of routes can be done according to actual needs when the page is loaded or after user operation.

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.

See all articles