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 22 of the Intermediate WordPress Plugins: REST API & React Admin course
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.

WordPressReactPerformanceOptimizationSelectorsphpplugin-development

Previously in this course, we covered writing selectors for data access to retrieve state from our custom data store. While those basic selectors work for simple data, they can trigger expensive re-renders in your admin dashboard if they return new object references every time they are called. Today, we’re leveling up: we will learn to optimize selector performance to ensure our Knowledge Base plugin remains responsive as our dataset grows.

The Problem: Why Selectors Cause Re-renders

In the WordPress Data module, a component subscribed to a selector will re-render whenever the value returned by that selector changes. This check is performed using a "shallow equality" comparison. If your selector returns a new object or array—even if the underlying data inside is identical—React assumes the state has changed and forces a re-render.

This is a classic performance trap. If your Knowledge Base dashboard has a list of 50 articles and your selector returns a fresh array object every time, every single list item component will re-render on every state update, even if the data for that specific item hasn't changed.

Implementing Shallow Comparison and Memoization

To prevent these unnecessary updates, we must ensure our selectors return the exact same reference if the input state hasn't changed. We do this using createSelector from the @wordpress/data package, which provides memoization by default.

Consider this unoptimized selector:

JAVASCRIPT
// A common performance pitfall
const getActiveArticles = (state) => {
    return state.articles.filter(article => article.status === CE9178">'publish');
};

Every time getActiveArticles is called, .filter() creates a new array instance. If you use this in a component via useSelect, that component will re-render every time any part of the global state changes, because the array reference is always "new."

Here is how we optimize it:

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

const getActiveArticles = createSelector(
    (state) => {
        return state.articles.filter(article => article.status === CE9178">'publish');
    },
    (state) => [state.articles] // Dependency array
);

The createSelector function takes two arguments: the transformation function and a dependency function. It only re-runs the transformation if the result of the dependency function changes. By returning [state.articles], we tell the store: "Only re-calculate the filtered list if the articles array reference itself changes."

Hands-on Exercise: Optimizing the Knowledge Base List

Let's apply this to our Knowledge Base plugin. Open your store/selectors.js file and find your article list selector.

  1. Refactor: Wrap your existing selector in createSelector.
  2. Define Dependencies: Identify exactly which part of the state tree your selector depends on.
  3. Verify: Use the React Developer Tools "Profiler" tab to record your dashboard interactions. Look for the "Why did this render?" badge. If you see "Props changed" for your list items, your selector is likely returning new references.

Common Pitfalls

  • Over-memoization: Don't wrap every trivial selector in createSelector. Memoization consumes memory. Only optimize selectors that return derived objects or arrays, or those that perform expensive computations.
  • Ignoring Dependency Arrays: If you pass an incorrect dependency (e.g., watching the wrong state slice), your UI will become stale and fail to update when data actually changes.
  • Anonymous Functions in Selectors: If you define the transformation function inline inside createSelector, make sure you aren't creating new function references that break the memoization cache.

Profiling Your Dashboard

To see these optimizations in action, you need to measure them. In your browser's DevTools:

  1. Open the Profiler tab.
  2. Click the "Record" button.
  3. Perform an action in your Knowledge Base dashboard (e.g., adding an article).
  4. Stop recording and inspect the "Ranked" chart.

If you see a component rendering that shouldn't be, check the selector it's using. If it's returning a fresh object, you've found your performance bottleneck. This is a critical step to take alongside understanding React re-rendering, as it ensures your plugin handles data-heavy admin interfaces with ease.

By mastering these techniques, you ensure that your WordPress plugins provide a professional, lag-free experience for site administrators. This is a core pillar of structuring state for performance in larger React applications.

Up next: We will tackle complex state dependencies where one selector needs to consume the output of another, ensuring our data remains consistent across the entire dashboard.

Previous lessonImplementing Resolvers for Data FetchingNext lesson Handling Complex State Dependencies
Back to Blog

Similar Posts

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.

Read more
WordPressJune 26, 20263 min read

Input Validation and Error Handling for WordPress REST API & React

Learn to build robust input validation for your WordPress plugin. Discover how to provide instant client-side feedback and handle server-side errors gracefully.

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 22 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
Read more
WordPressJune 26, 20264 min read

Handling Complex State Dependencies in WordPress Data Stores

Master complex state management by chaining selectors and coordinating cross-component dependencies in your WordPress Data store for consistent, clean UI logic.

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

    4 min
  • 25

    Advanced Sanitization Techniques

    3 min
  • 26

    Input Validation and Error Handling

    3 min
  • 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