Redux State改变后Navigator怎么没自动渲染screen React Navigation 5

发布于 2022-09-12 02:30:14 字数 2254 浏览 16 评论 0

AppNav.js

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

const Stack = createStackNavigator();

const AppNav = ({isAuthenticated}) => {
    const [logged, setLogged] = useState(false)

    useEffect(() => {
        if(isAuthenticated) {    
            console.log(logged);
          setLogged(true);
        } else {
          setLogged(false);
          console.log(logged);
        }
    },[isAuthenticated])



    return (
        <NavigationContainer>
            <Stack.Navigator>
            {   
                !logged?
                <Stack.Screen name='Auth' component={AuthScreen} options={{headerShown: false}} />
                :<Stack.Screen name='Home' component={HomeScreen} options={{headerShown: false}} />                
            }
            </Stack.Navigator>                    
        </NavigationContainer>
    );
}

const mapStateToProps = ({isAuthenticated}) => {
    return {
        isAuthenticated
    };
}
export default connect(mapStateToProps, null)(AppNav)

userAction.js

import {LOGIN_WITH_FACEBOOK} from './types';


export const loginWithFacebook = () => async(dispatch) => {        


                dispatch( { type: LOGIN_WITH_FACEBOOK, payload: {isAuthenticated: true} } );                    

}

userReducer.js

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

const INITIAL_STATE = {
    isAuthenticated: false    
};

const userReducer = (state=INITIAL_STATE, action) => {
    switch(action.type){
        case LOGIN_WITH_FACEBOOK: 
            return {...state, ...action.payload};
        default: 
            return state;
    }
}
export default userReducer

rootReducer.js

import {combineReducers} from 'redux';
import userReducer from './userReducer';


const rootReducer = combineReducers({
    user: userReducer
 })
 export default rootReducer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2022-09-19 02:30:14

找到原因了,combineReducer那边是user: userReducer, 要state.user才能连接到。

AppNav.js

const mapStateToProps = (state) => {
    return {
        isAuthenticated: state.user.isAuthenticated
    };
}
export default connect(mapStateToProps, null)(AppNav)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文