Blog>
Snippets

Using createSelector Hook for Memoized Selectors

Show an example of using the createSelector hook from Redux v5.0.0 for creating memoized selectors in a React-Redux component.
import { useSelector, createSelector } from 'react-redux';

const selectNumOfTodos = createSelector(
  state => state.todos,
  todos => todos.length
);

const TodoComponent = () => {
  const numOfTodos = useSelector(selectNumOfTodos);
  return <div>Number of todos: {numOfTodos}</div>;
};
This code defines a memoized selector using createSelector from Redux. The selector calculates the number of todos. In the TodoComponent, useSelector is used with the created selector to get the number of todos and render it.
import { createSelector } from 'reselect';
import { useSelector } from 'react-redux';

// Assuming you have a state shape with an user object
const selectUser = state => state.user;
const selectUserName = createSelector(
  selectUser,
  user => user.name
);

const UserNameComponent = () => {
  const userName = useSelector(selectUserName);
  return <div>User name: {userName}</div>;
};
Here, createSelector is used to create a memoized selector that extracts the user's name from the state. In UserNameComponent, useSelector is utilized with the selectUserName selector to access and display the user's name.
import { createSelector } from 'reselect';

// The base selector that gets the whole users array
const selectUsers = state => state.users;

// createSelector to get only active users
const selectActiveUsers = createSelector(
  [selectUsers],
  users => users.filter(user => user.isActive)
);

// createSelector to get the count of active users
const selectActiveUserCount = createSelector(
  [selectActiveUsers],
  activeUsers => activeUsers.length
);
This code block creates two chained memoized selectors with createSelector. The first one, selectActiveUsers, filters out active users. The second one, selectActiveUserCount, computes the number of active users.
import React from 'react';
import { useSelector } from 'react-redux';

// The provided selector from the previous code block
// const selectActiveUserCount = ...;

const ActiveUserCountComponent = () => {
  const count = useSelector(selectActiveUserCount);
  return <div>Active User Count: {count}</div>;
};
This React component ActiveUserCountComponent uses useSelector to retrieve the count of active users from the store using the previously defined selectActiveUserCount selector.
/* Example CSS */

/* Apply some basic styling to the components */

/* For TodoComponent */
.todo-component {
  font-size: 16px;
  color: #333;
}

/* For UserNameComponent and ActiveUserCountComponent */
.user-info-component {
  font-size: 16px;
  color: blue;
  margin-bottom: 10px;
}
This is an example of how the CSS might look to style the TodoComponent, UserNameComponent, and ActiveUserCountComponent with some basic font size and color.
<!-- Example HTML markup -->

<!-- This should be included within a React component's render method or function component's return statement -->

<div className="todo-component"><TodoComponent /></div>
<div className="user-info-component"><UserNameComponent /></div>
<div className="user-info-component"><ActiveUserCountComponent /></div>
This HTML markup shows how the components could be rendered in a React application, wrapped with div elements and the appropriate class names for styling.