Prevent Redundant State in JavaScript Applications Using Selectors
Tags: javascript, react, redux, node
Published: 2020-08-07 12:15:36

---
title: Prevent Redundant State in JavaScript Applications Using Selectors
published: true
description:  State management is challenging. We can make it less challenging by making sure we don’t store any redundant information in our state.
tags: javascript, react, redux, node
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/4228308hj6cibuy4y7br.png
canonical_url: https://typeofnan.dev/calculating-derived-state-in-javascript-using-selectors/
---

State management is challenging. We can make it less challenging by making sure we don’t store any redundant information in our state. 

*Please give this post a 💓, 🦄, or 🔖 if it you learn something new!*

<hr />

I make other easy-to-digest tutorial content! Please consider:

- Subscribing to my [DevTuts mailing list](https://buttondown.email/devtuts)
- Subscribing to my [DevTuts YouTube channel](https://youtube.com/c/devtutsco)

<hr />

Let’s say in our program we need to figure out whether people will be allowed in our bar. We can determine this by examining a couple attributes of the person: we can look at his or her age (anyone who is 21 or older may enter the bar) or we can look at whether he or she is an employee of the bar (all bar employees are allowed to enter, regardless of age). Now, we could store all this information in our state object:

```javascript
const state = {
  name: 'Joe',
  age: 15,
  employee: false,
  allowedIn: false,
};
```

The problem here is that `allowedIn` can easily be derived from the `age` and `employee` props, meaning it is technically redundant with that information. This is most problematic because it presents an opportunity for our state to contradict itself.

# Introducing Selectors

We can use selectors to solve this issue. Selectors are functions that take state as a property and return the derived state value. Let's see if we can create a selector to replace our `allowedIn` property.

```javascript
const state = {
  name: 'Joe',
  age: 15,
  employee: false,
};

const allowedIn = state => state.age >= 21 || state.employee;
```

Now we see that, if we ever need to determine if the person is allowed in to our bar, we can simply use the boolean result of calling `allowedIn(state)`!

# Diving Deeper with Composable Selectors

Now what if we have some more complex requirements? Perhaps we need to make a decision called `highFiveThem` based on whether they are allowed in to the bar and they are friendly. Let's first pretend we have a new state object that includes whether they are friendly.

```javascript
const state = {
  name: 'Judy',
  age: 22,
  employee: false,
  isFriendly: true,
};
```

Our decision is not just based on our state object anymore, but is based on the result of another selector as well. This is where we start using higher order functions to compose selectors from other selectors. Let’s look at how this works in practice and then we can take a peek under the hood.

```javascript
const state = {
  name: "Judy",
  age: 22,
  employee: false,
  isFriendly: true
};
const allowedIn = state => state.age >= 21 || state.employee;
const isFriendly = state => state.isFriendly;
const highFiveThem = createSelector(
    allowedIn,
    isFriendly,
    (allowedIn, isFriendly) => allowedIn && isFriendly;
)
highFiveThem(state);
// true
```

This will essentially calculate the result of the `allowedIn(state)` and `isFriendly(state)` selectors and make those inputs to the final function passed to `createSelector`.

Academically, let’s take a look at how this higher order function could work.

```javascript
const createSelector = (...funcs) => {
  const last = funcs.pop();
  return state => {
    const inputs = funcs.map(func => func(state));
    return last(...inputs);
  };
};
```

How this works:

- The `createSelector` function takes any number of `funcs`.
- We create a variable called `last` to store the last function passed to `createSelector`. (The last function will receive the results of all the other functions as arguments).
- We return a function (our new selector!).
- Whenever that last function is executed, we map over all the input functions to determine their results based on the passed state.
  We return the value of our last function given the results of all previous functions.

Pretty neat right?

# Thinking About Efficiency and usage in React

Many selector libraries (e.g., Reselect for Redux) include additional functionality to memoize selector results. This is because it’s inefficient to recalculate a selector if its input hasn’t changed. Mapping our that memoization functionality here is a bit out of scope, but just keep in mind that it is likely beneficial to use one of these libraries due to this kind of optimization (versus rolling your own selector solution). Often, you'll see a combination of React, Redux, and Reselect used to efficiently calculate derived state in frontend-heavy applications.