Skip to content

Instantly share code, notes, and snippets.

@carlinisaacson
Created February 1, 2018 02:23
Show Gist options
  • Save carlinisaacson/dfce5fa1e3de5654d9b11b2f04c21575 to your computer and use it in GitHub Desktop.
Save carlinisaacson/dfce5fa1e3de5654d9b11b2f04c21575 to your computer and use it in GitHub Desktop.
Android hardware back handling in ignite
import React from 'react'
import { BackHandler } from 'react-native' // <<<<<<<<<<< ADDED THIS
import * as ReactNavigation from 'react-navigation'
import { connect } from 'react-redux'
import AppNavigation from './AppNavigation'
// Change to component to get lifecycle functions
// here is our redux-aware smart component
class ReduxNavigation extends React.Component {
componentDidMount () {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress)
}
componentWillUnmount () {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress)
}
handleBackPress = () => {
const { dispatch } = this.props
// optional exit app handler
// const shouldExitApp = code to determine if app should exit (e.g. can't go back anymore)
// if (shouldExitApp) return false
dispatch(ReactNavigation.NavigationActions.back())
return true
}
render () {
const { dispatch, nav } = this.props
const navigation = ReactNavigation.addNavigationHelpers({
dispatch,
state: nav
})
return <AppNavigation navigation={navigation} />
}
}
const mapStateToProps = state => ({ nav: state.nav })
export default connect(mapStateToProps)(ReduxNavigation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment