react-router (v4) como voltar?
Tentando descobrir como posso voltar à página anterior. estou usando[react-router-v4][1]
Este é o código que configurei na minha primeira página de destino:
<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>
Para encaminhar para as páginas subsequentes, basta:
this.props.history.push('/Page2');
No entanto, como posso voltar à página anterior? Tentei algumas coisas como mencionado abaixo, mas sem sorte: 1.this.props.history.goBack();
Dá erro:
TypeError: null não é um objeto (avaliando 'this.props')
this.context.router.goBack();
Dá erro:
TypeError: null não é um objeto (avaliando 'this.context')
this.props.history.push('/');
Dá erro:
TypeError: null não é um objeto (avaliando 'this.props')
Publicando oPage1
código aqui abaixo:
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;