A brief discussion on several ways to pass parameters in react routing

A brief discussion on several ways to pass parameters in react routing

The first parameter passing method is dynamic routing parameter passing.

By setting the path attribute of the link, the route is passed. When the link tag is clicked, the entire URL passed will be displayed in the URL address above

<Link to='/home?name=dx'>Home</Link>

If you want to actually get the passed parameters, you need to do it in the corresponding subcomponent
this.props.location.search gets the string and then manually parses it

Because the parameters can be seen by the user, it is troublesome to pass and obtain them, so it is not recommended

The second parameter passing method: implicit routing parameter passing

<Link to={{
     pathname: 'about',
     state: {
      name: 'dx'
     }
    }}>About</Link>

The so-called implicit route parameter passing means that the parameter information is not exposed in the URL. When you click the link tag, if you want to get the passed parameters, you can get them through this.props.location.state in the corresponding route component.

Recommended, safer, and more convenient to obtain the passed parameters

The third parameter passing method is to pass parameters between components

When to use?
When a routing component needs to receive parameters from a parent component

Modify the way route tags activate components through component attributes. Normally, route tags are used in routing.

//Succinct and clear, but unable to receive parameters from parent component <Route path='/test' component={Test}></Route>

After the transformation

<Link to='/test'>Test</Link>
<Route path='/test' render={(routeProps) => {
 //routeProps is the parameter return passed by the routing component (
     //In the case of the original routing component parameters, expand the binding of the parameters passed by the parent component to the child component <Test {...routeProps} name='dx' age={18} />
     )
 }}></Route> 



When the link tag is clicked, this.props in the corresponding test subcomponent obtains the parameters passed from the parent component and the parameters of the routing component itself. It is highly recommended that the parameters are slightly troublesome to pass, and the parameters received are very convenient. In addition, the parameters of the routing component itself can still be received, which is safe and will not be seen by users.

The fourth parameter passing method withRouter high-order component binds routing parameters to child components

When to use withRouter?

If you want to get the route parameters in a subcomponent, you must use the subcomponent of the route tag in the route to be bound to the route parameters.

In order to solve the problem of obtaining routing parameters for subcomponents that are not bound by route tags, you need to use withRouter

Generally used on buttons such as return to home page and return to previous level.

import React from 'react';
import BackHome from './backhome';
export default class Test extends React.Component {
 render () {
  console.log(this.props)
  return (
   <div>
    This is the content of the test //The button to return to the home page is not rendered through the route tag, so there is no route parameter in this.props of the subcomponent <BackHome>Return to the home page</BackHome> 
   </div>
  )
 }
}

import React from 'react';
//Import withRoute
import {withRouter} from 'react-router-dom';
class BackHome extends React.Component {
 goHome = () => {
  //The component must have routing parameters and methods in this.props when using withRouter //Otherwise, an error will be reported this.props.history.push({
   pathname: '/home',
   state: {
    name: 'dx' //Similarly, you can pass parameters to the component corresponding to the home route through state}
  })
 }
 render () {
  return (
   <button onClick={this.goHome}>this.props.children</button>
  )
 }
}
//When exporting, use the withRouter tag to pass the backHome component as a parameter export default withRouter(BackHome)

It is very important when you need to use it, so it is still recommended.

This concludes this article on several ways to pass parameters in react routing. For more relevant content on passing parameters in react routing, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of React routing parameter transfer method summary record
  • React component communication routing parameter transfer (react-router-dom)

<<:  Summary of ten principles for optimizing basic statements in MySQL

>>:  Linux kernel device driver virtual file system notes

Recommend

How to use axios to filter multiple repeated requests in a project

Table of contents 1. Introduction: In this case, ...

Discussion on horizontal and vertical centering of elements in HTML

When we design a page, we often need to center th...

Example to explain the size of MySQL statistics table

Counting the size of each table in each database ...

js to achieve simulated shopping mall case

Friends who are learning HTML, CSS and JS front-e...

React gets input value and submits 2 methods examples

Method 1: Use the target event attribute of the E...

Steps to transfer files and folders between two Linux servers

Today I was dealing with the issue of migrating a...

React Native scaffolding basic usage detailed explanation

Build the project Execute the command line in the...

Summary of essential Docker commands for developers

Table of contents Introduction to Docker Docker e...

Detailed explanation of MySQL DEFINER usage

Table of contents Preface: 1.Brief introduction t...

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

MySQL database must know sql statements (enhanced version)

This is an enhanced version. The questions and SQ...

MySQL query learning basic query operations

Preface MySQL is the most popular relational data...

Talk about important subdirectory issues in Linux system

/etc/fstab Automatically mount partitions/disks, ...

Linux kernel device driver address mapping notes

#include <asm/io.h> #define ioremap(cookie,...