我的reducer返回undefined,就像没有将其添加到CombineReducers一样,但是

时间:2019-09-30 19:54:38

标签: javascript reactjs redux react-redux

一切似乎都还不错,我已经调试了一段时间,但似乎不明白问题出在哪里

reducers / index.js

// @flow
import { combineReducers } from "redux";
import blocks from "./blocks";
import users from "./users";
import add from "./add";
const rootReducer = combineReducers({
    blocks,
    users,
    add
});
export default rootReducer;

reducers / add.js

// @flow
import AnyAction from "redux";
import * as ActionTypes from "./../constants";

const initialState = {
    number: 0
};

export default function Add(state = initialState, action) {
    switch (action.type) {
        case ActionTypes.ADD:
            return Object.assign({}, state, {
                number: number + 1
            });
        default:
            return state;
    }
}

连接功能

const { navigation, blocks, dispatch, number } = this.props;
.
.
.
const mapStateToProps = state => ({
    blocks: state.blocks,
    number: state.number
});

export default connect(mapStateToProps)(BlockList);


1 个答案:

答案 0 :(得分:1)

在合并化简器之后,您需要使用合并的化简器名称在mapStateToProps中访问化简器的状态。代替:

const mapStateToProps = state => ({
   blocks: state.blocks,
   number: state.number
});

具有此:

const mapStateToProps = state => ({
  blocks: state.blocks,
  number: state.add.number
});

现在,使用state.add.number,您可以访问add.js归约器中的数字状态。

我试图在这里重新创建您的问题:

Edit loving-tharp-fe3ti