如何使用 React Navigation 5 导航到 Action Creators 内的屏幕

Kev*_*Lee 2 reactjs react-native react-redux react-navigation react-navigation-v5

React Navigation 5 Auth Flow中,它表示当条件状态发生变化时屏幕将自动导航。

  • 我已将屏幕设置为当
    isAuthenticated 的状态更改为 true 时导航到 HomeScreen。
  • 控制台中没有错误。isAuthenticated 确实更改为 true,但状态更改后屏幕未导航到主屏幕。
  • 我还尝试在操作创建器中使用 {NavigationActions} 和 {CommonActions} 等替代方案。强制导航,但也不起作用。

AuthStackNav.js

import React from 'react';
import { connect } from 'react-redux';
import {createStackNavigator} from '@react-navigation/stack';
import AuthScreen from '../screens/AuthScreen';
import HomeScreen from '../screens/HomeScreen';

const AuthStackNav = ({isAuthenticated}) => {
    const AuthStack = createStackNavigator();

    return (        
        <AuthStack.Navigator initialRouteName='Auth'>
        {
            isAuthenticated ?
            <AuthStack.Screen name='Home' component={HomeScreen} />                
            :
            <AuthStack.Screen  name='Auth' component={AuthScreen} />      
        }                       
                         
        </AuthStack.Navigator>
        
    );
};
const mapStateToProps = ({isAuthenticated}) => {
    return {isAuthenticated};
};
export default connect(mapStateToProps, null)(AuthStackNav);
Run Code Online (Sandbox Code Playgroud)

用户操作.js

import {LOGIN_WITH_FACEBOOK} from './types';
import {NavigationActions} from 'react-navigation'; 
import { CommonActions } from '@react-navigation/native'; 

export const loginWithFacebook = () => (dispatch) => {        
    dispatch({ type: LOGIN_WITH_FACEBOOK, payload: {isAuthenticated: true} });   
    dispatch(NavigationActions.navigate({routeName:'Home'}));
    dispatch( CommonActions.navigate({ name: 'Home' }) );                                           
};
Run Code Online (Sandbox Code Playgroud)

userReducer.js

import {LOGIN_WITH_FACEBOOK} from '../actions/types.js';

const initialState = {
    isAuthenticated: false    
};

const userReducer = (state=initialState, action) => {
    switch(action.type){
        case LOGIN_WITH_FACEBOOK: 
            return {...state, ...action.payload};
        default: 
            return state;
    }
}
export default userReducer;
Run Code Online (Sandbox Code Playgroud)

AuthScreen.js

import React from 'react';
import {Text, View, StyleSheet, Image, TouchableOpacity, SafeAreaView} from 'react-native';
import { connect } from 'react-redux';
import {loginWithFacebook} from '../actions/userActions';

const AuthScreen = ({loginWithFacebook}) => {    

    return (
        <View style={styles.screenContainer}>                

                <TouchableOpacity                     
                    activeOpacity={0.5}
                    onPress={loginWithFacebook}
                >  Facebook Login</TouchableOpacity> 

        </View>  
    );
};
const mapDispatchToProps = {
    loginWithFacebook
};
export default connect(null, mapDispatchToProps)(AuthScreen);
Run Code Online (Sandbox Code Playgroud)

AppNav.js

import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import AuthStackNav from './AuthStackNav';

const AppNav = () => {
    return (
        <NavigationContainer>
            <AuthStackNav />
        </NavigationContainer>
    );
};

export default AppNav;
Run Code Online (Sandbox Code Playgroud)

Kev*_*Lee 5

I was able to solve it with this guide.

RootNavigation.js

import * as React from 'react';

export const navigationRef = React.createRef();

export function navigate(name, params) {
  navigationRef.current?.navigate(name, params);
}
Run Code Online (Sandbox Code Playgroud)

AppNav.js

import { navigationRef } from './RootNavigation.js';
const AppNav = () => {
    return (
        <NavigationContainer ref={navigationRef} >
            <AuthStackNav />
        </NavigationContainer>
    );
};

export default AppNav;
Run Code Online (Sandbox Code Playgroud)

userActions.js

export const loginWithFacebook = () => (dispatch) => {        
    dispatch({ type: LOGIN_WITH_FACEBOOK, payload: {isAuthenticated: true} });   
    RootNavigation.navigate('Home');                                    
};
Run Code Online (Sandbox Code Playgroud)