Where should I handle sorting in Redux App?

ZPPP picture ZPPP · Dec 26, 2015 · Viewed 21.1k times · Source

I have an action / reducer / components. In one of my components (component dump) I have a Select. I get information on what type of filter my store. Where can I handle it in action, or reducer?

Answer

IMO, the right place to sort data is not directly in the reducers but in the selectors.

From redux docs:

Computing Derived Data

Reselect is a simple library for creating memoized, composable selector functions. Reselect selectors can be used to efficiently compute derived data from the Redux store.

I'm currently using selectors to filter and sort data.

  1. No data repetition in the state. You don't have to store a copy of the item sorted by one specific way.
  2. The same data can be used in different components, each one using a different selector function to sort for example.
  3. You can combine selector applying many data computations using selector that you already have in the application.
  4. If you do right, your selectors will be pure functions, then you can easily test them.
  5. Use the same selector in many components.