Skip to content

Instantly share code, notes, and snippets.

@AlexBrasileiro
Last active September 25, 2018 11:28
Show Gist options
  • Save AlexBrasileiro/49375e107365121a316bafc5e6e5477d to your computer and use it in GitHub Desktop.
Save AlexBrasileiro/49375e107365121a316bafc5e6e5477d to your computer and use it in GitHub Desktop.
React Native | Android Backbutton HOC
import React, { Component } from 'react';
import { BackHandler } from 'react-native';
const AndroidBackButton = (WrappedComponent) => {
return class extends Component {
constructor(props) {
super(props)
}
componentDidMount() {
this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
this.props.history.goBack() // for react-router-dom -> https://github.com/ReactTraining/react-router
return true;
});
}
componentWillUnmount() {
this.backHandler.remove();
}
render() {
return <WrappedComponent {...this.props} />
}
}
}
export default AndroidBackButton
// then use: withRouter(AndroidBackButton(YOURComponent))
@AlexBrasileiro
Copy link
Author

@burdiuz perfect approach to ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment