React返回頁面有3種方式,分別是:1、通過“this.props.history.push('/home');”方式返回到上一級頁面;2、通過“this.props.history.replace('/home');”方式返回頁面;3、通過“window.history.back(-1);”返回頁面。
前端(vue)入門到精通課程:進入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點擊使用
本教程操作環(huán)境:windows7系統(tǒng)、react18.0.0版、Dell G3電腦。
React返回頁面的幾種方式?
react 移動端返回上一級頁面的寫法
移動端返回上一級頁面的寫法:
import React, {Component} from 'react'; import './style.less'; class Header extends Component { clickBackHandler (){ // 返回到上一級頁面的幾種方法 //第一種 this.props.history.push('/home'); //第一種 this.props.history.replace('/home'); 但這兩種方法都不好 //第三種方法,推薦使用 window.history.back(-1); } render() { return ( <div id="common-header"> {/*Header 公共頭組件*/} <span className="back-icon"> <i className="icon-chevron-left" onClick={ this.clickBackHandler }></i> </span> <h1>{ this.props.title }</h1> </div> ); } } export default Header;
登錄后復(fù)制
推薦學(xué)習(xí):《react視頻教程》