React Navigation Preventing Going back to loading screen, reset not working

后端 未结 4 679
無奈伤痛
無奈伤痛 2021-01-21 13:32

I have a React Native application which I have implemented. Currently the app opens up on a loading screen which after mounting checks the firebase.auth().onAuthStateChang

4条回答
  •  难免孤独
    2021-01-21 14:10

    Try a custom navigation component with custom back button support. Dont forget to add the reducer to yoru combine reducers function.

    Create a navigation component:

    import React, { Component } from 'react';
    import { BackHandler } from 'react-native';
    import { connect } from 'react-redux';
    import { addNavigationHelpers } from 'react-navigation';
    import MainNavigator from './MainNavigator';
    
    class AppWithNavigationState extends Component {
      componentDidMount () {
        BackHandler.addEventListener('hardwareBackPress', () => {
          this.props.dispatch({
            type: 'Navigation/BACK'
          });
    
          return true;
        });
      }
    
      componentWillUnmount () {
        BackHandler.removeEventListener('hardwareBackPress');
      }
    
      render () {
        return (
          
        );
      }
    }
    
    export default connect((state) => ({ nav: state.nav }))(AppWithNavigationState);
    

    Create a navigation reducer:

    import { NavigationActions } from 'react-navigation';
    import MainNavigator from './MainNavigator';
    import { NAVIGATION_ON_SIGN_IN } from '../redux/actions/ActionTypes';
    import { BackHandler } from 'react-native';
    
    const initialState = MainNavigator.router.getStateForAction(MainNavigator.router.getActionForPathAndParams('loading'));
    
    function appShouldClose (nextState) {
      const { index, routes } = nextState;
    
      return index === 0 || routes[1].routeName === 'auth';
    }
    
    export default (state = initialState, action) => {
      const { router } = MainNavigator;
      let nextState;
    
      switch (action.type) {
        case NavigationActions.BACK:
          nextState = router.getStateForAction(action, state);
          appShouldClose(nextState) && BackHandler.exitApp();
          break;
    
        default:
          nextState = router.getStateForAction(action, state);
      }
    
      return nextState || state;
    };
    

提交回复
热议问题