减速机总是返回初始状态

时间:2018-10-05 17:33:44

标签: reactjs react-native redux react-redux

我一直在使用react-native和redux玩,但遇到错误。尽管当我在化简器中调试有效负载时,状态总是被初始化为起始状态。

这是我的减速器文件

let initialState = {
filterList: [],
isFetching: false,
activeFilters: [],
}


export function fetchFilterList(state = initialState, action) {
    return { ...state, isFetching: true };
}

export function fetchFilterListSuccess(state, action) {
    return {
        ...state,
        filterList: action.payload,
        isFetching: true,
        dsad: "dada",
    }
}

export function fetchFilterListError(state, action) {
    return { ...state, isFetching: false };

}

在这里我将它们组合为一个函数(与上面的文件相同):

export function combinedFiltersReducers(state = initialState, action) {
switch (action.type) {
    case ACTION_TYPES.FETCH_FILTER_LIST:
        return fetchFilterList(state, action);

    case ACTION_TYPES.FETCH_FILTER_LIST_SUCCESS:
        return fetchFilterListSuccess(state, action);

    case ACTION_TYPES.TOGGLE_FILTER_ITEM:
        return toggleFilterItemStart(state, action);

    case ACTION_TYPES.TOGGLE_FILTER_ITEM_SUCCESS:
        return toggleFilterItemSuccess(state, action);

    default:
        return state;
}

}

这是我的组合减速器功能,位于单独的文件中,称为主减速器。

    export default combineReducers({
    adList: fetchAdListSuccess,
    filterList: combinedFiltersReducers,
});

这是我在组件中接收状态的位置,它总是会下降到初始状态。

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

const mapPropsToDispatch = dispatch => ({
    fetchFilterList:() => dispatch(fetchFilterList()),
    toggleFilterItem: (data) => dispatch(toggleFilterItem(data)),
});

export default connect(
    mapStateToProps,
    mapPropsToDispatch
)(FilterComponent);

我找不到错误,所以我需要一些帮助。预先感谢。

2 个答案:

答案 0 :(得分:1)

好的,我发现了一个问题。举个例子:

return fetchFilterList(state, action);

您正在调用fetchFilterList方法并传递初始状态。因此,每次调用它实际上都会经过初始状态。该方法只是复制初始状态。宁愿这样:

return fetchFilterList(...state, action);

答案 1 :(得分:1)

我将假设您的ACTION_TYPES没有适合您正在调用的函数的类型,因此它将恢复为默认情况。

相关问题