試圖弄清楚我該如何回到上一頁。我使用[react-router-v4][1]
react-router(v4)如何回去?
這是我在第一次登陸頁面中配置的代碼:
<Router>
<div>
<Link to="/"><div className="routerStyle"><Glyphicon glyph="home" /></div></Link>
<Route exact path="/" component={Page1}/>
<Route path="/Page2" component={Page2}/>
<Route path="/Page3" component={Page3}/>
</div>
</Router>
爲了轉發到隨後的頁面,我簡單地做:
然而,如何我可以回到上一頁嗎? 試過幾件事情像下文提到的,但沒有運氣: 1. this.props.history.goBack();
給出錯誤:
TypeError: null is not an object (evaluating 'this.props')
this.context.router.goBack();
this.props.history.push('/');
給出錯誤:
TypeError: null is not an object (evaluating 'this.context')
給出錯誤:
TypeError: null is not an object (evaluating 'this.props')
在這裏張貼的Page1
代碼如下:
import React, {Component} from 'react';
import {Button} from 'react-bootstrap';
class Page1 extends Component {
constructor(props) {
super(props);
this.handleNext = this.handleNext.bind(this);
}
handleNext() {
this.props.history.push('/page2');
}
handleBack() {
this.props.history.push('/');
}
/*
* Main render method of this class
*/
render() {
return (
<div>
{/* some component code */}
<div className="navigationButtonsLeft">
<Button onClick={this.handleBack} bsStyle="success">< Back</Button>
</div>
<div className="navigationButtonsRight">
<Button onClick={this.handleNext} bsStyle="success">Next ></Button>
</div>
</div>
);
}
export default Page1;
什麼幾件事情,你有試過嗎? –
嘗試 'this.props.history.goBack();' https://github.com/ReactTraining/react-router/blob/dc2149ec0c63bfc95b71e40c81431e34cfbfeda9/packages/react-router-redux/modules/actions.js#L27 – baskax
@VivekDoshi:添加了什麼我想隨着錯誤,我遇到 –