2016-06-20 17 views
0

Das ist mein reducrer:redux Aufruf Minderer mit Wirkung

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

Meine Wurzel Minderer:

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

Jetzt habe ich:

const rootReducer = combineReducers({ 
    dashboard 
}); 

In der Komponente, das die mapStateToProps ist 2 Aktionen GET_ALERTS_SUCCESS und GET_WL_STATISTICS_SUCCESS. In der Komponente habe ich die Requisiten für actions.alerts, aber wie kann ich einen Verweis auf action.wlStatistics in der Komponente bekommen? Kann ich den Reducer mit einem Aktionstyp aufrufen?

Antwort

1

Ihr DashboardReducer gibt entweder 'alerts' initialState OR 'alerts' oder 'wlStatistics' für den nächsten Status zurück. Es sollte ein Objekt mit diesen beiden Handlungs Nutzlasten als Eigenschaften zurück:

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

Ihre Requisiten wird nun als

this.props.alerts 

und den Zustand

this.props.wlStatistics 

Immer wenn entweder Aktion aktualisiert abgebildet werden in der 'dashboardReducer', wird Ihre Komponente re-render/receiveProps und die Requisiten werden mit den neuen Werten

aktualisiert
Verwandte Themen