Home  >  Article  >  Web Front-end  >  How to implement forward and backward in react-router

How to implement forward and backward in react-router

藏色散人
藏色散人Original
2022-12-20 14:17:062329browse

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>

2. Event jump

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

Route forward goBac()

goback(){
      this.props.history.goBack()  
  }

Route rollback push()

go(){
      this.props.history.go(-1) 
  }

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!

Statement:
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