Home > Web Front-end > Front-end Q&A > How to implement forward and backward in react-router

How to implement forward and backward in react-router

藏色散人
Release: 2022-12-20 14:17:06
Original
2389 people have browsed it

React-router implements forward and backward methods: 1. Implement routing forward through "goback(){this.props.history.goBack()}"; 2. Through "go(){this.props. history.go(-1) }" to implement routing rollback.

How to implement forward and backward in react-router

#The operating environment of this tutorial: Windows 10 system, react18 version, Dell G3 computer.

How does react-router implement forward and backward?

React-routing jump method and routing forward and back

1. Tag jump

//1.引入依赖
import { Link  } from 'react-router-dom'
//2.在需要的地方加上这段,Link标签是a标签
<Link to={{ pathname: /*你的路由*/ }} replace> </Link>
Copy after login

2. Event jump

//1.引入依赖
import { withRouter } from &#39;react-router-dom&#39;;
//2.比如使用点击事件
fun(){
    this.props.history.push(&#39;/*你的路由*/&#39;)
  }
export default withRouter (App);
Copy after login

Route forward goBac()

goback(){
      this.props.history.goBack()  
  }
Copy after login

Route rollback push()

go(){
      this.props.history.go(-1) 
  }
Copy after login

Recommended learning: "react video tutorial"

The above is the detailed content of How to implement forward and backward in react-router. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template