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

How can MySQL effectively prevent database deletion and running away?

Table of contents Safe Mode Settings test 1. Upda...

How to use javascript to do simple algorithms

Table of contents 1 Question 2 Methods 3 Experime...

Web Design: Web Music Implementation Techniques

<br />When inserting music into a web page, ...

Detailed explanation of Nginx access restriction configuration

What is Nginx access restriction configuration Ng...

MySQL database index order by sorting detailed explanation

Table of contents The cause of the incident Anato...

Example of using CSS filter to write mouse over effect

Use CSS filter to write mouse over effect <div...

Introduction to MySQL overall architecture

The overall architecture of MySQL is divided into...

How to realize vertical arrangement of text using CSS3

In a recent project, I wanted to align text verti...

Method of dynamically loading geojson based on Vue+Openlayer

Load one or more features <template> <di...

MySQL database aggregate query and union query operations

Table of contents 1. Insert the queried results 2...

MySQL slow query method and example

1. Introduction By enabling the slow query log, M...

Detailed explanation of Tomcat directory structure

Table of contents Directory Structure bin directo...

JavaScript implements three common web effects (offset, client, scroll series)

Table of contents 1. Element offset series 2. Ele...