Skip to content

Instantly share code, notes, and snippets.

@iesteban
Forked from jankalfus/ReduxNavigation.js
Last active January 31, 2018 20:27
Show Gist options
  • Save iesteban/724227c2dc1364066473dee5bbfc304d to your computer and use it in GitHub Desktop.
Save iesteban/724227c2dc1364066473dee5bbfc304d to your computer and use it in GitHub Desktop.
Multiple routers with Drawer ReduxNavigation
import React from "react"
import * as ReactNavigation from "react-navigation"
import { connect } from "react-redux"
import AppNavigation from "./AppNavigation"
import { BackHandler } from "react-native"
import PropTypes from "prop-types"
class ReduxNavigation extends React.Component {
static propTypes = {
dispatch: PropTypes.func.isRequired,
nav: PropTypes.object.isRequired,
}
_isDrawerOpen = nav => nav.routes[0].index === 1
_shouldCloseApp = nav => {
if (nav.index > 0) return false;
if (nav.routes) {
return nav.routes.every(this._shouldCloseApp);
}
return true;
}
_goBack = () => this.props.dispatch(ReactNavigation.NavigationActions.back())
_closeDrawer = () => this.props.dispatch(ReactNavigation.NavigationActions.navigate({
routeName: "DrawerClose"
}))
_handleBackPress = () => {
if (this._isDrawerOpen(this.props.nav)) {
this._closeDrawer()
return true
}
if (this._shouldCloseApp(this.props.nav)) {
return false
}
this._goBack()
return true
}
componentWillMount() {
BackHandler.addEventListener("hardwareBackPress", this._handleBackPress)
}
componentWillUnmount() {
BackHandler.removeEventListener("hardwareBackPress", this._handleBackPress)
}
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