天天看點

React Native redux和react-navigation配合實體鍵盤傳回問題

constructor(props) {

    super(props);

    this.state = {        

    }

    this.onBackPress = this.onBackPress.bind(this);

  }

  componentDidMount() {

    SplashScreen.hide();

    BackHandler.addEventListener('hardwareBackPress', this.onBackPress);

  }

  componentWillUnmount() {

     BackHandler.removeEventListener('hardwareBackPress', this.onBackPress)

  }

  onBackPress = () => {

    const {dispatch, nav} = this.props;

    if (nav.routes[0].index === 0) {//如果RootNavigator中的MainNavigator的index為0,則不處理傳回事件

        ToastAndroid.show('善贊提示您:已經退出應用!', ToastAndroid.SHORT);

        return false;

    }

    dispatch(NavigationActions.back());

    return true;

  };

繼續閱讀