Last active
January 25, 2021 15:27
-
-
Save Jmzp/da80152d6c51ca787fa4b4aae6db125c to your computer and use it in GitHub Desktop.
HOC to manage the back android button behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback } from 'react'; | |
import { BackHandler } from 'react-native'; | |
import PropTypes from 'prop-types'; | |
import { useFocusEffect } from '@react-navigation/native'; | |
const AndroidBackHandlerWrapper = (props) => { | |
useFocusEffect( | |
useCallback(() => { | |
const { onBackPress } = props; | |
BackHandler.addEventListener('hardwareBackPress', onBackPress); | |
return () => BackHandler.removeEventListener('hardwareBackPress', onBackPress); | |
}, [props.onBackPress]), | |
); | |
const { children } = props; | |
return children; | |
}; | |
AndroidBackHandlerWrapper.propTypes = { | |
onBackPress: PropTypes.func, | |
children: PropTypes.node.isRequired, | |
}; | |
AndroidBackHandlerWrapper.defaultProps = { | |
onBackPress: () => {}, | |
}; | |
export default AndroidBackHandlerWrapper; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment