redux呼叫减速器带动作

时间:2016-06-20 11:18:23

标签: javascript reactjs redux

这是我的reducrer:

List

我的根减速器:

export default function dashboardReducer(state=initialState.alerts, action){
    switch(action.type){
        case constActions.GET_ALERTS_SUCCESS:
            return action.alerts;
        case constActions.GET_WL_STATISTICS_SUCCESS:
            return action.wlStatistics;
    default:
        return state;
    }
};

在组件中,这是mapStateToProps:

const rootReducer = combineReducers({
    dashboard
});

现在我有2个动作function mapStateToProps(state, ownProps){ return{ alerts: state.dashboard }; } GET_ALERTS_SUCCESS。 在组件中我有GET_WL_STATISTICS_SUCCESS的道具,但是如何在组件中获得对actions.alerts的引用?我可以用动作类型调用减速器吗?

1 个答案:

答案 0 :(得分:1)

您的dashboardReducer会返回'提醒' initialState或'警告'或者' wlStatistics'为下一个州。它应该返回一个具有这两个动作有效负载的对象作为属性:

const initialState = {
  alerts: null,
  wlStatistics: null
};

export default function dashboardReducer(state=initialState, action){
    switch(action.type){
        case constActions.GET_ALERTS_SUCCESS:
            return Object.assign({}, state, { action.alerts });
        case constActions.GET_WL_STATISTICS_SUCCESS:
            return Object.assign({}, state, { action.wlStatistics });
    default:
        return state;
    }
};

您的道具现在将映射为

this.props.alerts

this.props.wlStatistics

每当任一操作更新“dashboardReducer”中的状态时,您的组件将重新呈现/ receiveProps,并且道具将使用新值进行更新