@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
UsecreateReduxStore and register to create a store:
string
required
Unique store identifier (e.g., ‘my-shop’)
ReduxStoreConfig
required
Store configuration object:
reducer: State update functionactions: Action creator functionsselectors: State selector functionsresolvers: Async data fetching functionsinitialState: 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
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:Resolution Status
Check if resolvers have run:Resolution Selectors
isResolving
isResolving
Returns true if resolution is in progress.
hasStartedResolution
hasStartedResolution
Returns true if resolution has been triggered.
hasFinishedResolution
hasFinishedResolution
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 withcreateSelector:
Thunks
Action creators that dispatch multiple actions:Higher-Order Components
withSelect
Inject data as props:withDispatch
Inject action creators as props:Combined
Best Practices
Use TypeScript
Use TypeScript
Define types for better type safety:
Keep Selectors Pure
Keep Selectors Pure
Selectors should only read state, never modify it:
Normalize State Shape
Normalize State Shape
Store data by ID for efficient lookups:
Use combineReducers
Use combineReducers
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
- Subscribe behavior: Only calls listeners when state actually changes
- Async built-in: Resolvers handle data fetching without middleware
- Multiple stores: Registry pattern vs single store
- React HOCs:
withSelectandwithDispatchsplit fromconnect
TypeScript
Full TypeScript support:Dependencies
Core dependencies:redux(5.0.1) - Core Redux libraryrememo- Memoization library@wordpress/element- React abstraction@wordpress/compose- HOC utilities@wordpress/is-shallow-equal- Comparison utilities
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