在redux action creator里如何转换屏幕 React Navigation 5

发布于 2022-09-12 02:29:05 字数 1631 浏览 13 评论 0

userActions.js

import {LOGIN_WITH_FACEBOOK} from './types';

export const loginWithFacebook = () => (dispatch) => {        
    dispatch({ type: LOGIN_WITH_FACEBOOK, payload: {isAuthenticated: true} });                                              
};

AuthStackNav.js

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);

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;

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:29:05

我按照这个档案解决的 https://reactnavigation.org/d...

RootNavigation.js

import * as React from 'react';

export const navigationRef = React.createRef();

export function navigate(name, params) {
  navigationRef.current?.navigate(name, params);
}

AppNav.js

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

export default AppNav;

userActions.js

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