forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackButton.js
39 lines (32 loc) · 868 Bytes
/
BackButton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React from "react";
import PropTypes from "prop-types";
import { BackHandler } from "react-native";
class BackButton extends React.Component {
static contextTypes = {
router: PropTypes.shape({
history: PropTypes.shape({
goBack: PropTypes.func.isRequired,
index: PropTypes.number.isRequired
}).isRequired
}).isRequired
};
componentDidMount() {
BackHandler.addEventListener("hardwareBackPress", this.handleBack);
}
componentWillUnmount() {
BackHandler.removeEventListener("hardwareBackPress", this.handleBack);
}
handleBack = () => {
const { history } = this.context.router;
if (history.index === 0) {
return false; // home screen
} else {
history.goBack();
return true;
}
};
render() {
return this.props.children || null;
}
}
export default BackButton;