Skip to main content

@wordpress/data

The @wordpress/data package is WordPress’ central state management system. It provides a Redux-like architecture with additional features like resolvers for async data fetching and built-in integration with React.

Installation

Current Version: 10.40.0This package assumes an ES2015+ environment. Include the polyfill from @wordpress/babel-preset-default if needed.

What is @wordpress/data?

WordPress data serves as a hub for managing application state. It provides:
  • Centralized state management - Single source of truth for application data
  • Modular stores - Independent stores for different concerns
  • Async resolution - Built-in support for data fetching
  • React integration - Hooks and HOCs for components
  • Type safety - Full TypeScript support
While inspired by Redux, @wordpress/data includes unique features like resolvers and a modular store pattern. Don’t think of it as just “Redux for WordPress”.

Core Concepts

Stores

A store contains:
  • State - The data
  • Selectors - Functions to read state
  • Actions - Functions to update state
  • Reducer - Function that updates state
  • Resolvers - Functions to fetch external data

Selectors

Functions that retrieve and derive data from state:

Actions

Functions that return action objects to update state:

Reducers

Functions that update state based on actions:

Resolvers

Functions that fetch external data when selectors are called:

Creating a Store

Use createReduxStore and register to create a store:
string
required
Unique store identifier (e.g., ‘my-shop’)
ReduxStoreConfig
required
Store configuration object:
  • reducer: State update function
  • actions: Action creator functions
  • selectors: State selector functions
  • resolvers: Async data fetching functions
  • initialState: Optional preloaded state

Using Stores

select() - Reading Data

Retrieve data using selectors:

dispatch() - Updating Data

Update state using actions:
Action creators returned by dispatch() return promises when called.

subscribe() - Watching Changes

Listen for state changes:

React Integration

useSelect Hook

Retrieve data in components:
Function
required
Function called on state changes. Receives select function and returns value to expose to component.
Array
Dependency array for memoization (like useEffect)
*
Value returned by mapSelect function
Always include the dependency array to avoid unnecessary re-renders.

useDispatch Hook

Access action creators in components:
string | StoreDescriptor
Store name (e.g., ‘my-shop’). If omitted, returns registry.dispatch.
Object
Object containing bound action creators

Combined Example

Async Data with Resolvers

Resolvers automatically fetch data when selectors are called:
Using it:

Resolution Status

Check if resolvers have run:

Resolution Selectors

Returns true if resolution is in progress.
Returns true if resolution has been triggered.
Returns true if resolution has completed.

Advanced Features

Registry Selectors

Selectors that can access other stores:

Batching Updates

Batch multiple updates to avoid unnecessary re-renders:

Memoized Selectors

Create memoized selectors with createSelector:

Thunks

Action creators that dispatch multiple actions:

Higher-Order Components

withSelect

Inject data as props:

withDispatch

Inject action creators as props:

Combined

Best Practices

Define types for better type safety:
Selectors should only read state, never modify it:
Store data by ID for efficient lookups:
Split complex reducers:

Comparison with Redux

Similarities

  • Unidirectional data flow
  • Actions and reducers
  • Immutable updates
  • Middleware support

Differences

  • Built-in async with resolvers
  • Modular store pattern
  • Registry system
  • React integration included

Key Differences

  1. Subscribe behavior: Only calls listeners when state actually changes
  2. Async built-in: Resolvers handle data fetching without middleware
  3. Multiple stores: Registry pattern vs single store
  4. React HOCs: withSelect and withDispatch split from connect

TypeScript

Full TypeScript support:

Dependencies

Core dependencies:
  • redux (5.0.1) - Core Redux library
  • rememo - Memoization library
  • @wordpress/element - React abstraction
  • @wordpress/compose - HOC utilities
  • @wordpress/is-shallow-equal - Comparison utilities
See package.json for the complete list.

Resources

Data Module Guide

Official WordPress documentation

What is WordPress Data?

In-depth conceptual guide

GitHub Repository

Source code and issue tracker

Redux Documentation

Learn Redux core concepts