The method of using js Code to Control Route Jump after React Router 4.0

now you need to implement a requirement in react. Every time a page requests a link / jump, you need to verify the current login status to the server. The function requesting the link is encapsulated in a tool called request.js (not a component). When authentication fails, you need to jump to the error page. You are required not to use window.location.href method, because parameters need to be passed but must not be exposed. HashHistory can achieve similar functions before version 4.0. So what are we going to do after version 4.0?

Mar.24,2021

use withRouter
withRouter high-level components, and provide history for you to use ~

import React from "react";
import {withRouter} from "react-router-dom";

class MyComponent extends React.Component {
.
myFunction () {
this.props.history.push ("/ some/Path");
}
.
}
export default withRouter (MyComponent);

that's all right

Menu