vendredi 15 février 2019

Redux change multiple states

Say I have a global state reducer that has this shape:

global reducer:

const globalInitialState = {
  userId: 0,
  year: 1900
}

Then I have a state reducer for data based upon those global parameters:

summary reducer:

User 10's data for 2018

const summaryDataInitialState = [{data for user 10, 2018}, {data}, {data}]; 

to update the "summary data" I have an async redux-thunk action:

Summary Actions:

getSummary = (userId, year) => {
  return async dispatch => {
    let data = await api.getData(userid, year);
    dispatch(success(data));
  }
}

But when you change the year globally, how should I trigger updating the summary data?

Should I call the summary action "getSummary" from my "updateYear" action or is this a bad pattern/design? I don't like how this ties "summaryActions" to the "globalActions". And further down the road, maybe lots and lots of "sub-state" would need to be changed when these globals are changed.

Aucun commentaire:

Enregistrer un commentaire