首页 > 解决方案 > 保持 redux store 状态不变性的最佳方法是什么?

问题描述

/reducers/index.js

import * as types from '../actions/ActionTypes';

var initialState = {
    categoryName: 'default name redux'
};



function re_modifyCategoryName(state=initialState, action) {
    switch(action.type) {
        case types.MODIFY_CATEGORY_NAME :
            return Object.assign({}, state, {
                categoryName: action.categoryNewName
            });

        default:
            return state;

    }
}

export {re_modifyCategoryName}

我肯定检查stateredux store是由 Redux-dev-tool 和 redux-logger 更新的,

在此处输入图像描述

在此处输入图像描述

mapStateToProps没有被调用。


PresentationalComponent.js

... 
const mapStateToProps = (state) => {
    console.log("inside category name", state.categoryName);
    return {categoryName: state.categoryName}
};

export default connect(mapStateToProps)(AnswerEachCategory);
...

当页面第一次呈现时,如您所见mapStateToProps"inside category name", state.categoryName显示在consolepresentational component正确地props.categoryNameinitialStateof 中获取redux store。但是,为什么redux store没有触发变化mapStateToProps

这个问题与stateredux 商店的不变性有关吗?


容器.js

...
const mapDispatchToProps = (dispatch) => ({

    onModifyCategoryName: (categoryNewName) => {
        dispatch(actions.modifyCategoryName(categoryNewName))
    }
})

export default connect(null, mapDispatchToProps)(ModifyCategoryNameModal)
...

标签: reactjsreduxreact-reduxredux-thunk

解决方案


推荐阅读