forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackButton.js
36 lines (30 loc) · 807 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
import React from "react";
import { BackHandler } from "react-native";
import { __RouterContext as RouterContext } from "react-router";
class BackButton extends React.Component {
handleBack = () => {
if (this.history.index === 0) {
return false; // home screen
} else {
this.history.goBack();
return true;
}
};
componentDidMount() {
BackHandler.addEventListener("hardwareBackPress", this.handleBack);
}
componentWillUnmount() {
BackHandler.removeEventListener("hardwareBackPress", this.handleBack);
}
render() {
return (
<RouterContext.Consumer>
{context => {
this.history = context.history;
return this.props.children || null;
}}
</RouterContext.Consumer>
);
}
}
export default BackButton;