Skip to content

Instantly share code, notes, and snippets.

@JodusNodus
Last active August 1, 2017 20:54
Show Gist options
  • Save JodusNodus/1f0c246f6933b69cae195e20f0ef84a5 to your computer and use it in GitHub Desktop.
Save JodusNodus/1f0c246f6933b69cae195e20f0ef84a5 to your computer and use it in GitHub Desktop.
class BackController extends Component {
static propTypes = {
onBack: PropTypes.func.isRequired
}
componentDidMount = () =>
BackHandler.addEventListener('hardwareBackPress', this.handleBack)
componentWillUnmount = () =>
BackHandler.removeEventListener('hardwareBackPress', this.handleBack)
handleBack = () => this.props.onBack()
render = () => null
}
class NavController extends Component {
static propTypes = {
show: PropTypes.bool.isRequired,
showNav: PropTypes.func.isRequired,
hideNav: PropTypes.func.isRequired
}
componentDidMount = () =>
this.props.show
? this.props.showNav()
: this.props.hideNav()
componentWillReceiveProps(nextProps){
if(!this.props.show && nextProps.show)
this.props.showNav()
if(this.props.show && !nextProps.show)
this.props.hideNav()
}
componentWillUnmount = () =>
this.props.show && this.props.hideNav()
render = () => null
}
class SomeView extends Component {
handleBack = () => {} // ? Do some navigation logic ?
render = () => (
<View>
<BackController onBack={this.handleBack} />
<NavController show />
<Text>Some text</Text>
</View>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment