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

Solve the scroll-view line break problem of WeChat applet

Today, when I was writing a small program, I used...

HTML5+CSS3 header creation example and update

Last time, we came up with two header layouts, on...

Import CSS files using judgment conditions

Solution 1: Use conditional import in HTML docume...

JavaScript parseInt() and Number() difference case study

Learning objectives: The two functions parseInt()...

Introducing the code checking tool stylelint to share practical experience

Table of contents Preface text 1. Install styleli...

How to make your JavaScript functions more elegant

Table of contents Object parameters using destruc...

Share CSS writing standards and order [recommended for everyone to use]

CSS writing order 1. Position attributes (positio...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

Comparison between Redis and Memcache and how to choose

I've been using redis recently and I find it ...

Nginx/Httpd load balancing tomcat configuration tutorial

In the previous blog, we talked about using Nginx...

Detailed explanation of DOM DIFF algorithm in react application

Table of contents Preface What is VirtualDOM? Rea...

Detailed explanation of the use of Vue.js render function

Vue recommends using templates to create your HTM...