Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 19 of the Intermediate WordPress Plugins: REST API & React Admin course
WordPressJune 25, 20263 min read

Writing Selectors for Data Access: Memoization in WordPress

Master selectors in @wordpress/data to efficiently retrieve state. Learn to implement memoization for high-performance React admin interfaces in WordPress.

WordPressReact@wordpress/dataSelectorsMemoizationphpplugin-development

Previously in this course, we explored registering a custom data store in WordPress, which set the foundation for managing our plugin's state. In this lesson, we move from defining the store to extracting data from it, ensuring our components only re-render when the specific data they depend on actually changes.

The Role of Selectors in @wordpress/data

In the WordPress data store architecture, selectors are pure functions that take the state as an argument and return a specific slice of data.

Directly accessing the store state inside a component is an anti-pattern. If you access a large object directly, your component will re-render whenever any part of that object changes, even if the specific property you care about remains identical. Selectors solve this by acting as a bridge, transforming raw state into the exact format your UI needs.

Implementing Memoization

Because selectors run every time the store changes, they must be performant. We use memoization to cache the result of a selector based on its arguments. If the arguments haven't changed, the selector returns the cached result instead of re-calculating it.

In the WordPress data ecosystem, we typically use createSelector from the @wordpress/data package, which is built on top of the popular reselect library.

Worked Example: Retrieving Knowledge Base Items

Let's assume our Knowledge Base store holds an array of items. We want a selector that filters these items by a specific category.

JAVASCRIPT
import { createSelector } from CE9178">'@wordpress/data';

// The base selector: returns the raw items from state
export const getKnowledgeBaseItems = (state) => {
    return state.items;
};

// The memoized selector: filters items by category
export const getItemsByCategory = createSelector(
    (state, category) => {
        const items = getKnowledgeBaseItems(state);
        return items.filter((item) => item.category === category);
    },
    (state, category) => [state.items, category] // Dependency array
);

In this example, createSelector accepts two arguments:

  1. The result function: The logic that computes the derived data.
  2. The dependency function: A function that returns an array of dependencies. If these dependencies match the previous run, the result function is skipped.

Querying Store State from Components

To use these selectors inside your React components, you use the useSelect hook. This hook automatically subscribes the component to the store and triggers a re-render when the selector's output changes.

JAVASCRIPT
import { useSelect } from CE9178">'@wordpress/data';
import { STORE_NAME } from CE9178">'./store';

const CategoryList = ({ category }) => {
    const items = useSelect((select) => {
        return select(STORE_NAME).getItemsByCategory(category);
    }, [category]);

    if (!items.length) return <p>No items found.</p>;

    return (
        <ul>
            {items.map(item => <li key={item.id}>{item.title}</li>)}
        </ul>
    );
};

Hands-on Exercise

  1. Open your Knowledge Base store definition file.
  2. Implement a getArchivedItems selector that returns only items where status === 'archived'.
  3. Use createSelector to ensure this filtering logic is memoized.
  4. Update your Dashboard component to use useSelect and display a count of archived items in a header summary.

Common Pitfalls

  • Violating Purity: Selectors must remain pure. Never perform side effects (like API calls or console.log with mutations) inside a selector. If you need to fetch data, use resolvers.
  • Over-selecting: Avoid creating one giant selector that returns the entire state object. Create granular selectors that return only what a specific component requires.
  • Dependency Array Mismatch: If you forget to include a variable used in your selector logic within the dependency array of createSelector, the selector will return stale data.
  • Ignoring useSelect: Manually subscribing to the store using subscribe is error-prone and inefficient. Always prefer useSelect.

Selectors are the primary way your UI communicates with your data. By keeping them pure and memoized, you ensure that your admin dashboard remains responsive even as your Knowledge Base grows.

Up next: Defining Actions and Reducers to modify the store state.

Previous lessonRegistering a Custom Data StoreNext lesson Defining Actions and Reducers
Back to Blog

Similar Posts

WordPressReactJune 25, 20263 min read

Performance Optimization with Selectors in WordPress React Apps

Master React Performance Optimization in WordPress. Learn how to implement shallow comparison and memoized selectors to prevent unnecessary re-renders.

Read more
WordPressJune 25, 20263 min read

Implementing Resolvers for Data Fetching in WordPress @wordpress/data

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 19 of 45

  1. 1

    Setting up the WordPress Development Environment

    3 min
  2. 2

    Introduction to @wordpress/scripts

    3 min
  3. 3

    Configuring ESLint and Prettier

    3 min

Learn how to implement resolvers in @wordpress/data to automate API data fetching. Discover how to sync your React state with the WordPress REST API seamlessly.

Read more
WordPressJune 25, 20263 min read

Registering a Custom Data Store in WordPress @wordpress/data

Learn to register a custom data store in WordPress using @wordpress/data. Master createReduxStore and store registration to centralize your plugin's state.

Read more
4

Localizing Data for JavaScript

3 min
  • 5

    Anatomy of a REST API Endpoint

    3 min
  • 6

    Implementing REST API Permission Callbacks

    3 min
  • 7

    Handling GET Requests in REST API

    3 min
  • 8

    Validating and Sanitizing API Arguments

    4 min
  • 9

    Creating POST Endpoints for Data Submission

    3 min
  • 10

    Updating Existing API Resources

    3 min
  • 11

    Handling Asynchronous State in React

    3 min
  • 12

    Building the Knowledge Base Service Layer

    3 min
  • 13

    Scaffolding the React Admin Dashboard

    3 min
  • 14

    Working with @wordpress/components

    3 min
  • 15

    Creating a React Form for Submissions

    3 min
  • 16

    Implementing CRUD in the Admin UI

    3 min
  • 17

    Understanding WordPress Data Store Architecture

    4 min
  • 18

    Registering a Custom Data Store

    3 min
  • 19

    Writing Selectors for Data Access

    3 min
  • 20

    Defining Actions and Reducers

    3 min
  • 21

    Implementing Resolvers for Data Fetching

    3 min
  • 22

    Optimizing Performance with Selectors

    3 min
  • 23

    Handling Complex State Dependencies

    4 min
  • 24

    Implementing Nonce Verification

    Coming soon
  • 25

    Advanced Sanitization Techniques

    Coming soon
  • 26

    Input Validation and Error Handling

    Coming soon
  • 27

    Protecting Admin Screens

    Coming soon
  • 28

    Production Build Pipeline

    Coming soon
  • 29

    Debugging React in the WordPress Admin

    Coming soon
  • 30

    Building Search and Filter Functionality

    Coming soon
  • 31

    Internationalization in React

    Coming soon
  • 32

    Managing File Uploads via REST API

    Coming soon
  • 33

    Optimizing API Response Times

    Coming soon
  • 34

    Working with Date and Time in React

    Coming soon
  • 35

    Implementing Drag-and-Drop Sorting

    Coming soon
  • 36

    Creating Custom Hooks for API Logic

    Coming soon
  • 37

    Integrating with Gutenberg Blocks

    Coming soon
  • 38

    Handling Conflict Resolution

    Coming soon
  • 39

    Building a Modal Confirmation System

    Coming soon
  • 40

    Implementing Activity Logging

    Coming soon
  • 41

    Using Webpack Aliases

    Coming soon
  • 42

    Unit Testing API Endpoints

    Coming soon
  • 43

    Unit Testing React Components

    Coming soon
  • 44

    Handling Large Datasets with GraphQL

    Coming soon
  • 45

    Implementing Real-time Updates with Web

    Coming soon
  • View full course