omriman12
omriman12

Reputation: 1700

redux call reducer with action

This is my reducrer:

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;
    }
};

My root reducer:

const rootReducer = combineReducers({
    dashboard
});

In the component, this is the mapStateToProps:

function mapStateToProps(state, ownProps){
    return{
        alerts: state.dashboard
    };
}

Now I have 2 actions GET_ALERTS_SUCCESS and GET_WL_STATISTICS_SUCCESS. In the component I have the props for actions.alerts, but how can I get a reference to action.wlStatistics in the component? can i call the reducer with an action type?

Upvotes: 0

Views: 170

Answers (1)

Mark
Mark

Reputation: 6081

Your dashboardReducer either returns 'alerts' initialState OR 'alerts' OR 'wlStatistics' for the next state. It should return an object with both of those action payloads as properties:

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;
    }
};

Your props will now be mapped as

this.props.alerts

and

this.props.wlStatistics

Whenever either action updates the state in the 'dashboardReducer', your component will re-render/receiveProps and the props will be updated with the new values

Upvotes: 1

Related Questions