首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >构造Redux状态

构造Redux状态
EN

Stack Overflow用户
提问于 2019-06-11 02:59:48
回答 1查看 14关注 0票数 0

我是Redux的新手,在使用Redux时遇到了一些麻烦,所以到目前为止,我所做的可能是错误的,但请耐心等待。总而言之,我希望我的应用程序有一个非常简单的redux状态,只有两个键,"user“和”want“。我设置所有内容的方法是将状态默认为"user:{user: null}“和”have:{landing: true}“。我想知道为什么它被复制成这样,而不只是"user: null“和"landing: true"?

我的减速器:

代码语言:javascript
复制
const initialUserState = {
    user: null
};
const initialUiState = {
    landing: true
};

export function userReducer(state, action){
    if (typeof state === "undefined"){
        return initialUserState;
    }
    switch(action.type){
        case SIGN_IN: 
            return {...state, user: action.userName};
        case SIGN_OUT: 
            return {...state, user: null};
        default: 
            return state;
    }
}

export function uiReducer(state, action){
    if (typeof state === "undefined"){
        return initialUiState;
    }
    switch(action.type){
        case LANDING: 
            return {...state, landing: true};
        case NOT_LANDING: 
            return {...state, landing: false};
        default: 
            return state;
    }
}

以及减速机的组合:

代码语言:javascript
复制
import {userReducer, uiReducer} from "./reducers";
import {combineReducers} from "redux";

export default combineReducers({
    user: userReducer, 
    landing: uiReducer
});

同样,我只希望看到"user: null“和"landing: true",但我在连接中包装的组件中通过控制台注销了状态,我看到了"user:{user: null}”和"landing:{landing: true}“。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56532082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档