I have a Navigator
in an Android react native application.
I\'m using navigator.push()
to navigate to a different page. It would seem natural t
Don't forget bind [this]
The correct answer should be:
export default class MyPage extends Component {
constructor(props) {
super(props)
this.navigator = null;
this.handleBack = (() => {
if (this.navigator && this.navigator.getCurrentRoutes().length > 1){
this.navigator.pop();
return true; //avoid closing the app
}
return false; //close the app
}).bind(this) //don't forget bind this, you will remember anyway.
}
componentDidMount() {
BackAndroid.addEventListener('hardwareBackPress', this.handleBack);
}
componentWillUnmount() {
BackAndroid.removeEventListener('hardwareBackPress', this.handleBack);
}
render() {
return (
{this.navigator = navigator}}
...