How to use history redirection in React Router

How to use history redirection in React Router

In react-router, the jump in the component can be used with <Link>

But how to jump outside the component requires the use of react routing history

The replace method is used in the same way as the push method. The function of replace is to replace the current history record.
go, this method is used to move forward or backward, history.go(-1);
goBack, this method is used to go back, history.goBack();
goForward, this method is used to move forward, history.goForward();

1.hook

import {useHistory} from 'react-router-dom';
function goPage(e) {
 history.push({
 pathname: "/home",
 state: {id: 1}
 });
}

pathname is the routing address, state can be passed as a parameter

Get parameters:

import {useLocation} from 'react-router-dom';
function getParams(){
let location = useLocation();
let id = location.state.id;
}

2. Class component

import React from 'react';
import {createBrowserHistory} from "history";
 
class App extends React.Component {
  constructor(props) {
      super(props);
    }
   goPage() {
 let history = createBrowserHistory()
 history.push({
 pathname: "/home",
 state: {id: 1}
 });
    history.go();
 }
  render() {return null;}
 
}

If history.go is not called, the route changes, but the page does not jump.

This is the end of this article about how to use history jump in React Router. For more relevant React Router history jump content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • React-router v4: How to use history to control route jumps
  • react-router JS control routing jump example
  • Example of how react-router implements jump value transfer
  • React Router 5.1.0 uses useHistory to implement page jump navigation

<<:  Detailed explanation of Mybatis special character processing

>>:  Solution to the inconsistency between crontab execution time and system time

Recommend

Docker uses nextcloud to build a private Baidu cloud disk

Suddenly, I needed to build a private service for...

Summary of the differences between count(*), count(1) and count(col) in MySQL

Preface The count function is used to count the r...

HTML tag dl dt dd usage instructions

Basic structure: Copy code The code is as follows:...

Interactive experience trends that will become mainstream in 2015-2016

The most important interactive design article in ...

How to install Django in a virtual environment under Ubuntu

Perform the following operations in the Ubuntu co...

Detailed process of NTP server configuration under Linux

Table of contents 1. Environment Configuration 1....

MySQL knowledge points for the second-level computer exam mysql alter command

Usage of alter command in mysql to edit table str...

React realizes secondary linkage effect (staircase effect)

This article shares the specific code of React to...

How to upload projects to Code Cloud in Linux system

Create a new project test1 on Code Cloud Enter th...

MySQL 5.7.24 installation and configuration method graphic tutorial

MySQL is the most popular relational database man...

How to implement on-demand import and global import in element-plus

Table of contents Import on demand: Global Import...

Tutorial on how to remotely connect to MySQL database under Linux system

Preface I recently encountered this requirement a...

mysql update case update field value is not fixed operation

When processing batch updates of certain data, if...

How to encapsulate axios in Vue project (unified management of http requests)

1. Requirements When using the Vue.js framework t...

Detailed explanation of the usage of the ESCAPE keyword in MySQL

MySQL escape Escape means the original semantics ...