Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

javascript - 在Modal的子组件中设置BackAndroid / BackHandler事件侦听器不会触发(BackAndroid/BackHandler Event Listeners are not Triggered when set in a Modal's Child Component)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This behaviour is documented on React Native's site :(此行为记录在React Native的网站上 :)

onRequestClose The onRequestClose callback is called when the user taps the hardware back button on Android or the menu button on Apple TV.(onRequestClose当用户点击Android上的硬件后退按钮或Apple TV上的菜单按钮时,将调用onRequestClose回调。) Because of this required prop, be aware that BackHandler events will not be emitted as long as the modal is open.(由于有这个必要的道具,请注意,只要打开模式,就不会发出BackHandler事件。) So you will need to use the onRequestClose function instead of BackHandler.(因此,您将需要使用onRequestClose函数而不是BackHandler。) <Modal visible={visible} onRequestClose={() => this.setState({ visible: false })} >

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...