我如何在反应本机中编程导航栏后键



一旦按下nav-bar back Back按钮,试图从socket.io连接进行编程断开。

我已经尝试根据Wix GitHub文档进行操作按钮按下:https://wix.github.io/react-native-navigation/#/docs/topbar-bar-bar-bar-bar-bar-bar-buttons?id=handling-button-button-press-exents

export default class Lobby extends React.Component {
  static options(passProps) {
    return {
      topBar: {
        leftButtons: {
          id: "backButton"
        }
      }
    };
  }
  constructor(props) {
    super(props);
    this.state = {
      username: "",
      queue: []
  };
    Navigation.events().bindComponent(this);
  }
// for the parameter I've tried: `{buttonId}`, `{backButton}`,`"backButton"`, `backButton`
      navigationButtonPressed(backButton) {
        const socket = io("http://172.31.99.250:3000");
         socket.emit("leaveLobby", this.state.username);
      }

处理程序功能的任何内容都没有发生。这些应用程序只是返回上一页,而无需发送socket.io事件

中只使用这样的使用

反应 - 纳入V2

   import {BackHandler} from 'react-native'
   ...

    constructor(props){
        super(props)
        Navigation.events().bindComponent(this)
    }
    componentDidDisappear() {
        BackHandler.removeEventListener('hardwareBackPress',this.handleBackPress);
    }
    componentDidAppear() {
        BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
    }
    handleBackPress = () => {
        this.moveToHome()
        return true
    }
    moveToHome = () => {
        Navigation.mergeOptions("maintabs", {
            bottomTabs: {
                  currentTabIndex: 0,
            }
      })
    }
...

您必须使用此方法如下以运行您的功能

navigationButtonPressed({ buttonId }) {
let isRTL = this.checklanguage();
switch (buttonId) {
  case 'menuBtn':     // id of your navigation item
    this.toggleSideMenu(isRTL);    // func to run  
    break;
  case 'logoutBtn':
    this.logoutHandler();
    break;
  default:
    break;
}

}

您的代码在对象上工作时具有错误

// for the parameter I've tried: `{buttonId}`, `{backButton}`,`"backButton"`, `backButton`
  navigationButtonPressed(backButton) {
    const socket = io("http://172.31.99.250:3000");
     socket.emit("leaveLobby", this.state.username);
  }

尝试我的一个

最新更新