[英]Override back button wix react native navigation V2?
我目前正在从 Wix RNN V1 过渡到 V2,到目前为止,我已经设法找到合适的替代 API,除了覆盖 Android 上的后退按钮。
在 V1 中,我们可以传递overrideBackPress: true
属性,然后在相应的屏幕上手动处理后退按钮按下。
但是,在 V2 中我没有找到这样的替代品,我能找到的唯一主题是这个线程:
https://github.com/wix/react-native-navigation/issues/4217
我已经在那里实施了建议,但是即使应该覆盖 Wix 导航,它仍然会自动关闭屏幕。
任何已知的解决方案?
我遇到了同样的问题,我可以在两个平台上覆盖 backpress 行为的唯一方法是用自定义按钮替换左后退按钮,并为 Android 中的硬件按钮使用 React Native 的 BackHandler。 代码如下。
组分 A
//Navigate to component B from A
Navigation.push(this.props.componentId, {
component: {
name: 'ComponentB',
options: {
topBar: {
leftButtons: [{
id: 'backPress',
text: 'Back',
icon: require('backbutton.png')
}]
},
}
}
});
组分 B
import React, { PureComponent } from 'react';
import { View, BackHandler } from 'react-native';
import { Navigation } from 'react-native-navigation';
export default class ComponentB extends PureComponent {
constructor(props) {
super(props);
Navigation.events().bindComponent(this);
}
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}
navigationButtonPressed({ buttonId }) {
switch (buttonId) {
case 'backPress': {
this.handleBackPress();
break;
}
}
}
handleBackPress = () => {
//Custom logic
//Go back if required
Navigation.pop(this.props.componentId)
//Stop the default navigation
return true;
};
//Render component
render() {
return (<View></View>);
}
}
您可以使用registerScreenPoppedListener
:
Navigation.events().registerScreenPoppedListener((event) => {
if (event.componentId === "my-screen-id") {
// do something
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.