Skip to content

Instantly share code, notes, and snippets.

@dotkebi
Forked from iesteban/ReduxNavigation.js
Created January 31, 2018 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotkebi/5e4e30da69d634b51ad571c5020bc0c5 to your computer and use it in GitHub Desktop.
Save dotkebi/5e4e30da69d634b51ad571c5020bc0c5 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