I am unable to set event listeners using the react-native BackHandler in a component that is placed inside a modal.(我无法在模态内放置的组件中使用react-native BackHandler设置事件侦听器。)
I suspect that this happens because the modal is listening to the method that is passed on the onRequestClose prop.(我怀疑这是因为模态正在侦听onRequestClose道具上传递的方法。)
Well, I am not sure if this is a bug or a feature request but I would suggest that you allowed us to pass a certain value (eg null) to the onRequestClose prop as a way of flagging that there might be BackHandler event listeners being set in the Modal's child components, and that these listeners have priority (ie override the onRequestClose of the Modal).(好吧,我不确定这是错误还是功能请求,但我建议您允许我们将某个值(例如null)传递给onRequestClose道具,以作为标记可能设置了BackHandler事件侦听器的方式在Modal的子组件中,并且这些侦听器具有优先级(即,覆盖Modal的onRequestClose )。)
Environment(环境)
Environment:(环境:)
OS: macOS High Sierra 10.13.3(操作系统:macOS High Sierra 10.13.3)
Node: 9.2.0(节点:9.2.0)
Yarn: 0.24.6(纱:0.24.6)
npm: 5.6.0(npm:5.6.0)
Watchman: 4.7.0(守望者:4.7.0)
Xcode: Xcode 9.2 Build version 9C40b(Xcode:Xcode 9.2 Build版本9C40b)
Android Studio: 3.0 AI-171.4408382(Android Studio:3.0 AI-171.4408382)
Packages: (wanted => installed)(软件包:(需要=>已安装))
react: 16.2.0 => 16.2.0(反应:16.2.0 => 16.2.0)
react-native: 0.53.0(反应本机:0.53.0) => 0.53.0(=> 0.53.0)
Steps to Reproduce(重现步骤)
Below there is the instructions inside the child component:(下面是子组件内部的说明:)
class ChildComponent extends Component {
componentDidMount () {
BackHandler.addEventListener('hardwareBackPress', this._onBackPress)
}
componentWillUnmount () {
BackHandler.removeEventListener('hardwareBackPress', this._onBackPress)
}
_onBackPress = () => {
console.log('Event was triggered')
return true
}
render () {
return (
<Text>{'Some Text'}</Text>
)
}
}
export default ChildComponent
The component that has the Modal (parent) has the following instructions:(具有模态(父级)的组件具有以下说明:)
class ParentComponentWithModal extends Component {
constructor (props) {
super(props)
this.state = {
modalVisible: true
}
}
render () {
const { modalVisible } = this.state
return (
<View>
<Modal
visible={modalVisible}
onRequestClose={() => console.log('onRequestClose')}
>
<ChildComponent />
</Modal>
</View>
)
}
}
export default ParentComponentWithModal
Expected Behavior(预期行为)
The _onBackPress method added to hardwareBackPressed listener should be executed when the back button is pressed.(当按下后退按钮时,应执行添加到hardwareBackPressed侦听器的_onBackPress方法。)
Actual Behavior(实际行为)
When the back button is pressed, the function defined on the onRequestClose prop is triggered.(当按下后退按钮时,将触发onRequestClose道具上定义的功能。) Even if no function is defined on the onRequestClose prop, the methods attached to the event listeners defined in the modal's children are not executed.(即使在onRequestClose道具上未定义任何函数,也不会执行附加到模态子级中定义的事件侦听器的方法。)
ask by Marcelo Dauane translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…