【问题标题】:Problem passing redux state to react-navigation prop using connect()使用 connect() 将 redux 状态传递给 react-navigation 道具的问题
【发布时间】:2021-06-28 10:57:43
【问题描述】:

我有一个需要使用 redux 状态的类组件,但我注意到使用 connect() 对 @react-navigation 根本不起作用。这是我当前的文件:

UserAuthenticationReducer

const initialState = null;

export let userAuthenticationReducer=(state = initialState, action)=>{
    switch(action.type){
        case "LOGIN":
            return {
                ...state,
                    user:{
                        name:"Bobbington",
                        lastName: "TheThird"
                    }
                };
        case "LOGOUT":
            return {
                ...state,user:null
            }
        default:
            return state;
    }
}
export default userAuthenticationReducer;

RootReducer

import userAuthenticationReducer from './Reducers';
import {combineReducers} from 'redux';

const rootReducer = combineReducers({
    userAuthenticationReducer,
});

export default rootReducer;

登录连接


import {login,logout} from '../ReduxStates/LoginActions'
export const mapUserAuthenticationStateToProps = state => {
    return { 
        user: state?.userAuthenticationReducer?.user
    };
};
export const mapUserAuthenticationDispatchToProps ={
    login,
    logout
}

应用程序

const rootStore = createStore(rootReducer)

const App =()=> {
    return (
        <Provider store={rootStore}>
            <MainRoutesContainer />
        </Provider>
    );
};

导航容器

import { NavigationContainer} from "@react-navigation/native";
import {createStackNavigator} from "@react-navigation/stack";
import {MainMenu} from "../Screens/MainMenu";
import React from 'react';


const Stack = createStackNavigator();
const StackRoute =()=>{
    return(
        <Stack.Navigator initialRouteName="MainMenu">
            <Stack.Screen name="MainMenu" component={MainMenu} />
        </Stack.Navigator>
    )
}

export const MainRoutesContainer=()=>{
    return(
        <NavigationContainer>
            <StackRoute />
        </NavigationContainer>
    )
}

主菜单


import { connect } from "react-redux"
import {mapUserAuthenticationStateToProps,mapUserAuthenticationDispatchToProps} from '../ReduxStates/LoginConnect'

export class MainMenu extends Component{

    getUserState=()=>{
        console.log(this.props.user)
    }

    render(){
        return(
            <Center flexDirection="column">
                <Text>
                    Main Menu
                </Text>
                <Button title="Log user" onPress={(event) => this.getUserState(event)}/>
            </Center>
        );
    }
}

export default connect(mapUserAuthenticationStateToProps,mapUserAuthenticationDispatchToProps)(MainMenu);

如果我记录 MainMenu 的 props.user,它将显示为未定义。在挖掘了一段时间后,我发现如果你这样做:

const StackRoute =()=>{
    let mainComponent = connect(mapUserAuthenticationStateToProps,mapUserAuthenticationDispatchToProps)(MainMenu);
    return(
        <Stack.Navigator initialRouteName="MainMenu">
            <Stack.Screen name="MainMenu" component={mainComponent} />
        </Stack.Navigator>
    )
}

它会起作用,但只有在我使用 MainMenu 组件中的 reducer 的函数之后,否则它仍然会在挂载时显示为 undefined。使用带有 useSelector 钩子的功能组件是可行的,但这不是我被允许使用的选项。我是在使用 connect() 时做错了什么,还是 React-Navigation v5 不适用于 redux 的 connect()?

【问题讨论】:

  • 您是否尝试过查看this 和相关页面?
  • 大部分文档都在使用功能组件。我尝试了他们在文档中使用类组件所做的一些事情,但没有奏效。

标签: reactjs react-native redux react-navigation react-navigation-v5


【解决方案1】:

您正在导入未连接的组件。您export default 已连接组件,但正在导入命名导出“MainMenu”,这是您未连接的组件。

你必须

import MainMenu from "../Screens/MainMenu";

而不是

import {MainMenu} from "../Screens/MainMenu";

【讨论】:

  • 这并没有修复它,状态仍然是未定义的。 Pictured here
  • 好吧,只要state?.userAuthenticationReducernull,这就是来自商店的正确值。你确定不是这样吗? (另外,更笼统:你在这里写的是一种非常古老的 Redux 风格 - 和 React。一般建议是使用带有函数组件的 redux 和 react-redux 钩子以及官方的 redux 工具包。你可能正在关注非常过时的资源。请关注官方教程:redux.js.org/tutorials/index)
  • 我尝试用其他值替换初始状态,但它是未定义的(我们都知道未定义!=null),但我想我会走向功能组件路线(即使在我的用例中确实不是一个选项,因为这只是一个示例,尽管我会尽力实现它)。
  • @ooatman your initialState整个对象设置为null。由于您使用的是可选链,state?.userAuthenticationReducer?.user 的初始值为undefined。你可能想要const initialState = {user: null}; 而不是const initialState = null;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
  • 2021-11-07
  • 2020-10-26
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多