Skip to content

Instantly share code, notes, and snippets.

@mehrankhandev
Created December 26, 2018 17:21
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 mehrankhandev/43795c69b260c2f7955894328973c5a1 to your computer and use it in GitHub Desktop.
Save mehrankhandev/43795c69b260c2f7955894328973c5a1 to your computer and use it in GitHub Desktop.
import React from "react";
import { BackHandler } from "react-native";
import { connect } from "react-redux";
import { NavigationActions } from "react-navigation";
import { App } from "../index";
class ReduxNavigation extends React.Component {
componentDidMount() {
BackHandler.addEventListener("hardwareBackPress", this.onBackPress);
}
componentWillUnmount() {
BackHandler.removeEventListener("hardwareBackPress", this.onBackPress);
}
onBackPress = () => {
const { nav, dispatch } = this.props;
if (nav.index === 0) {
return false;
}
dispatch(NavigationActions.back());
return true;
};
render() {
const { nav, dispatch } = this.props;
return <App state={nav} dispatch={dispatch} />;
}
}
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