调用mapStateToProps似乎传递了错误的状态对象

call to mapStateToProps appears to pass wrong state object

本文关键字:错误 状态 对象 mapStateToProps 调用      更新时间:2023-09-26

我正在开发我的第一个React-Redux应用程序。

这是我的减速器的代码(我只有一个):

const CalculationsReducers = (state = {}, action) => {
    switch (action.type) {
        case CalculationsActions.LOAD_CALCULATIONS:
            return Object.assign({}, state, {
                calculations: [{
                    id: 'abc',
                    name: 'test',
                    date: 'test',
                    status: 'in progress'
                }]
            });
        default:
            return state;
    }
};

这是我的'mapStateToProps'函数的代码,我使用connect()

const mapStateToProps = (s) => {
    return  {
        calculations: s.calculations ||[]
    };
};

当我分派一个load_calculation类型的动作时,我可以看到一个日志跟踪(使用react-logger),但是状态对象对我来说似乎很奇怪。有人能指出我的错误吗?

使用react-logger记录跟踪

工作流程看起来不错。state对象看起来也不错,但是在导入reducer来配置存储时,您可能需要更改别名,以使其更直观。

在你的配置存储文件中:

import calculations from 'reducers/CalculationReducer';

注意,它要求您将CalculationReducer作为默认值导出。然后你将在你的状态对象中看到calculations而不是CalculationReducer

我注意到你有一个payload随着你的行动,但从来没有在减速机处理。如果你需要它在状态对象中,那么在reducer中使用action.payload处理它。