Optimizing React-Redux Selectors with Reselect

Showcase how to use reselect to create memoized selectors that compute derived data, optimizing the performance for Redux v5.0.1 applications.
import { createSelector } from 'reselect';

// Assuming we have a state shape as follows:
// state = {
//   shop: {
//     products: []
//   },
//   user: {
//     preferences: {}
//   }
// }

// First, define simple selectors to access parts of the state
const getProducts = state =>;
const getUserPreferences = state => state.user.preferences;

// Then, create a memoized selector using Reselect
// This selector will not recompute unless products or preferences have changed
export const getFilteredProducts = createSelector(
  [getProducts, getUserPreferences],
  (products, preferences) => {
    // Implement filtering logic here
    // For example, filtering products based on user preferences
    return products.filter(product => product.color === preferences.favoriteColor);
This code defines two simple selectors: 'getProducts' and 'getUserPreferences' which are used to access parts of the state. It then creates a memoized selector called 'getFilteredProducts' using 'createSelector' from Reselect. The 'getFilteredProducts' selector will only recompute when 'products' or 'preferences' have changed, preventing unnecessary recalculations when other parts of the state update. This optimization is helpful for expensive computations like filtering a large list of products based on user preferences.