Redux: few lists with separate data managed by the same reducer

67 views Asked by At

So, my application has two (or more) lists of articles that are both managed by the ArticlesReducer. And I have two associated actions:

  1. LOAD_FAVOURITES
  2. LOAD_FEED

And the state is:

State = {
    feed: ArticleListState
    favourites: ArticleListState
}

So, basically those two sub-states should have completely separate data (retrieved from the REST api) but handled it in the same manner. The only difference is what data is being managed.

ArticleListState has various associated actions like ADD_ITEM, REMOVE_ITEM, UPDATE_ITEM.

I decided to make a wrapper around the Action that also has a value responsible for identifying which sub-state should be updated.

Like so:

RoutedAction
{
    action: Action,
    route: string
}

And my parent reducer is now checking the route property of the RoutedAction and passes action to the appropriate sub-reducer which handles it as usual.

Can it be considered a good practice? If not, how would such use-case be normally implemented?

0

There are 0 answers