-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigator.js
54 lines (45 loc) · 1.4 KB
/
Navigator.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @flow
import React from 'react';
import { BackHandler } from 'react-native';
import { connect } from 'react-redux';
import { addNavigationHelpers, NavigationActions, StackNavigator } from 'react-navigation';
import type { NavigationState } from 'react-navigation/src/TypeDefinition';
import HomeScreen from './src/containers/Home';
import TimeChangeScreen from './src/containers/TimeChange';
export const Navigator = StackNavigator({
Home: { screen: HomeScreen },
TimeChange: { screen: TimeChangeScreen },
});
type Props = {
nav: NavigationState,
dispatch: Function,
};
class NavigatorWithState extends React.Component<Props> {
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.onBack);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.onBack);
}
onBack = () => {
const backAction = NavigationActions.back();
const nav = Navigator.router.getStateForAction(backAction, this.props.nav);
if (nav && nav !== this.props.nav) {
this.props.dispatch(backAction);
return true;
}
return false;
};
render() {
return (
<Navigator
navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.nav,
})}
/>
);
}
}
const mapStateToProps = state => ({ nav: state.nav });
export default connect(mapStateToProps)(NavigatorWithState);