- BackHandler , :
BackHandler.addEventListener('hardwareBackPress', () => {
const { dispatch, nav } = this.props
if (nav.routes.length === 1 && (nav.routes[0].routeName === 'Login' || nav.routes[0].routeName === 'Start')) return false
dispatch({ type: 'Navigation/BACK' })
return true
})
, . true false . false . true .
, :
BackHandler.addEventListener('hardwareBackPress', () => {
const { dispatch, nav } = this.props
if (nav.routes[0].routeName === 'TriviaQuestion') return false
if (!playTimeLeft && (nav.routes[0].routeName === 'TriviaQuestion')) return false
if (nav.routes[0].routeName === 'InvasiveDialog') return false
dispatch({ type: 'Navigation/BACK' })
return true
})
, , Back:
import React, { Component } from 'react'
import { Platform, BackHandler } from 'react-native'
import { Provider, connect } from 'react-redux'
import { addNavigationHelpers } from 'react-navigation'
import { NavigationStack } from './navigation/nav_reducer'
import store from './store'
class App extends Component {
componentWillMount() {
if (Platform.OS !== 'android') return
BackHandler.addEventListener('hardwareBackPress', () => {
const { dispatch, nav } = this.props
if (nav.routes.length === 1 && (nav.routes[0].routeName === 'Login' || nav.routes[0].routeName === 'Start')) return false
dispatch({ type: 'Navigation/BACK' })
return true
})
}
componentWillUnmount() {
if (Platform.OS === 'android') BackHandler.removeEventListener('hardwareBackPress')
}
render() {
const { dispatch, nav } = this.props
const navigation = addNavigationHelpers({
dispatch,
state: nav
})
return <NavigationStack navigation={navigation} />
}
}
const mapStateToProps = ({ nav }) => ({ nav })
const RootNavigationStack = connect(mapStateToProps)(App)
const Root = () => (
<Provider store={store}>
<RootNavigationStack />
</Provider>
)
export default Root
, "" , . , , , , .
Redux, , .
NavigationStack export const NavigationStack = StackNavigator({ ...etc }) .