react-native-back-android icon indicating copy to clipboard operation
react-native-back-android copied to clipboard

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

Open schavaLogi opened this issue 7 years ago • 3 comments

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) }

schavaLogi avatar Feb 15 '18 06:02 schavaLogi

What is the order of connect and hardwareBackPress?

awesomejerry avatar Feb 22 '18 15:02 awesomejerry

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.

schavaLogi avatar Feb 23 '18 01:02 schavaLogi

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)
}

awesomejerry avatar Feb 23 '18 05:02 awesomejerry