Selectors
This terms is pretty descriptive — it’s something that you’d use to select
the data that you want from the store. You’ve probably already seen
code like this:
{
i18n: {
locale: 'en'
}
}
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 1/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
{
i18n: {
locale: {
name: 'English',
simpleCode: 'en',
fancyCode: 'en_EN'
}
}
}
This would mean we’d have to track down all the state.i18n.locale
uses in the app and adjust them to the new format and this might be
tricky and error-prone. How about if we had a helper function that we
could use instead? 🤔
Clear API
There’s no need for every component in your app to know the internal
schema of our data store.
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 2/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
deploy simple projects much faster), but with the project’s growth it
might turn into a nightmare (where we introduce bugs every time we
try to change anything).
Easier refactoring
As in the example above, using selector functions helps us refactor our
store schema much easier. We can keep the same API (the selector
functions that we use to access the state), but just change them
internally to operate on a new data format.
Selectors add a new level of abstraction, separating the data from the
code that accesses it. New levels of abstraction are not always
bene cial, but this one is easy to understand and not leaky, so I think
that’s as good as they get.
Performance
Finally, we can use the new level of abstraction to o er something that
every engineer likes: generalised performance improvements.
In a codebase where every component queries its data directly it’s very
di cult to o er performance improvements to data fetching. You’d
have to think of a x that would work everywhere and then go through
each of your components one by one, implementing it. That’s de nitely
not a fun way to spend your day.
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 3/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
Instead, using selectors we have a single interface that they all use, so
we can implement our performance xes directly at this level. The
obvious thing to do would be to o er data caching — if more
components ask for the same thing, we can keep its value cached /
memoized to serve it quicker, as long as the underlying data doesn’t
change.
As the old joke says, there are only a couple hard problems in computer
science, with caching being one of them, so let’s dive into this subject a
bit deeper.
For example let’s take our article-writing app that we talked about in
our Normalisation blog post. We can imagine that for such a simple app
we quickly run out of reasonable features to implement and we start
working on tasks that are as… let’s say sophisticated as “user can see the
list of the last tags they used”.
How would we get such a list if we don’t keep track of when tags are
used? Let’s say we choose to implement it like this:
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 4/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
In such a case we could use reselect to cache the result like this:
We can see that the calculations has been done only once, the second
and third results are served from cache.
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 5/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
That works pretty well in case of our simple (if a little crazy) example.
However, when we face more complicated real-world uses, we’ll
probably nd reselect ’s caching mechanism to be a little bit limiting
and we’d want more control over when and how do we cache the
results. In such a case we’d go one step deeper (or one level of
abstraction higher) and use re-reselect .
As you can see, that seems a bit over the top. Of course, we could create
a selector factory:
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 6/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
This is not only a bit verbose, but comes with a strong disadvantage.
Because such selectors would be dynamically created in each
component that uses them, they wouldn’t share the cache between
themselves (because they’re di erent selectors that just coincidentally
work the same way). This means that if we want to get the same
information in two di erent components, we’d have to instantiate two
selectors and compute it twice, not once.
With re-reselect , you can handle this problem in a better way. Let’s
rst take a look at the solution and we’ll break down the di erences
afterwards:
The output of this code is the following (notice that we only compute
the values twice and the last function call hits the already existing
cache):
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 7/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
There are two big di erences worth noting here. Let’s take a look how it
looks in a more general form:
we get parametrised cached selectors that we can reuse all over our app,
but still getting the advantage of the common cache that they share.
All of this with, arguably, cleaner syntax than a selector factory that
we’d have to use otherwise.
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 8/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
Most often, you’d use a string or a number as the cache key (as in the
example above). This would be typically a parameter (or a string being
a unique combination of di erent parameters) for the selector.
However, for more elaborate cases you have also an option to use a Map
object.
Of course, there are also various caching strategies (around cache size
and which values get expired rst) available for you to use and even a
possibility to create your own. It’s all controlled by the ( cacheObject
option).
Summary
After reading this blog post you’ve read how and why to use custom
selectors for your Redux store. We hope it motivated you to give a
closer look at how it’s implemented in your applications and make sure
you take advantage of caching to memoize any of the expensive
transformations that you make 💪
If that’s a topic that you found interesting, you should probably check
out also our blog post on data normalisation in Redux stores. As you
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 9/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
Also, stay tuned and make sure you subscribe to this blog to get our
weekly tips for creating better mobile applications faster 🚀
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 10/11
3/5/2019 Advanced Redux Patterns: Selectors – Brains & Beards
https://blog.brainsandbeards.com/advanced-redux-patterns-selectors-cb9f88381d74 11/11