awesomejerry / react-native-back-android

A Higher-order Component for handling back button press on React Native Android
MIT License
16 stars 5 forks source link

hardwareBackKey is not working for the connected components like using HOC connect #13

Open schavaLogi opened 6 years ago

schavaLogi commented 6 years ago

When we use connect react-redux component in stack navigator the back key not triggering function. import { connect } from "react-redux"; The below condition not matching event if i implement handleHardwareBackPress function in my connected component. if (this.refs.component.handleHardwareBackPress) { backAndroid.onHardwareBackPress(this.refs.component.handleHardwareBackPress) }

awesomejerry commented 6 years ago

What is the order of connect and hardwareBackPress?

schavaLogi commented 6 years ago

in case of hardwareBackPress at last it is not working becase the ref of component is coming as connect component so check fails.

if i do hardwareBackPress first and then return hoc pass to.connect it works.

awesomejerry commented 6 years ago

https://github.com/reactjs/react-redux/issues/475 https://github.com/reactjs/react-redux/blob/master/docs/api.md#arguments-1

Try connect(mapStateToProps, null, null, { withRef: true })

Looks like we should add getWrappedInstance to this.refs.component Maybe something like:

if (this.refs.component.handleHardwareBackPress) {
  backAndroid.onHardwareBackPress(this.refs.component.handleHardwareBackPress)
} else if (this.refs.component.getWrappedInstance) {
  backAndroid.onHardwareBackPress(this.refs.component.getWrappedInstance().refs['component'].handleHardwareBackPress)
}