Architecture
The data module is built upon and shares many core principles with Redux, but includes several distinguishing characteristics specific to WordPress:- Modular stores: Separate but interdependent stores with standardized patterns
- Selector-based access: Selectors as the primary entry point for data access
- Built-in async handling: Resolvers and thunks for side effects
- Entity-based system: Standardized approach to working with WordPress data
The data module follows Redux principles but is NOT just “Redux for WordPress”. It includes custom features like resolvers, entity management, and automatic cache invalidation.
Core Concepts
Stores
Stores are created usingcreateReduxStore and registered with register. Each store contains:
Selectors
Selectors retrieve and derive values from state. They accept state as the first argument, followed by any additional parameters:select function:
Actions
Actions are functions that return action objects to dispatch to the reducer. They describe state changes:dispatch function:
Action creators returned by dispatch return promises when called, allowing you to await their completion.
Resolvers
Resolvers are side effects for selectors. They fulfill data requirements the first time a selector is called:select( store ).getPrice( 'hammer' ) for the first time:
- The selector returns
null(no data yet) - The resolver triggers, fetching data from the API
- The resolver dispatches actions to store the data
- The selector now returns the fetched data on subsequent calls
Key Differences from Redux
Subscriptions
In@wordpress/data, subscribers are only called when state has actually changed, unlike Redux where subscribers are called on every dispatch.
Modular Pattern
Stores are registered with unique names and accessed through a central registry, enabling:- Cross-store selectors using
createRegistrySelector - Store-specific subscriptions
- Dynamic store registration
Split HOCs
withSelect and withDispatch are separate (unlike React Redux’s combined connect), reflecting that dispatch doesn’t depend on state subscriptions.
Core Data Entities
The@wordpress/core-data package provides a standardized interface for WordPress entities (posts, pages, users, etc.):
- Cache API responses
- Handle resolution status
- Track edits separately from persisted data
- Support undo/redo operations
React Integration
useSelect Hook
Retrieve data in React components:useDispatch Hook
Dispatch actions from React components:Performance Optimization
Batching Updates
Useregistry.batch() to group multiple updates and trigger listeners only once:
Memoized Selectors
UsecreateSelector for expensive computations:
Next Steps
- Working with Data - Learn useSelect and useDispatch patterns
- Entities & Undo/Redo - Understand entity operations
- Building Forms - Create data-driven forms