Master WordPress Data management. Learn to create custom stores, implement selectors and actions, and orchestrate global state across your blocks.
Previously in this course, we built modular React Component Architecture using functional components. While local useState is sufficient for simple UI toggles, professional WordPress plugins require a centralized, predictable state container to handle complex, cross-block interactions.
Today, we dive into @wordpress/data, the WordPress implementation of the Redux pattern. This library allows us to treat the entire WordPress admin as a single source of truth, enabling your Knowledge Base blocks to communicate effortlessly.
At its core, @wordpress/data uses a store-based architecture. A store is composed of four main pillars:
SET_KB_CATEGORY).If you are new to this pattern, understanding the WordPress Data store architecture is the prerequisite for writing maintainable, bug-free plugin interfaces.
To manage our Knowledge Base plugin's configuration globally, we must register a custom data store. We use createReduxStore to define our namespace and initial state.
JAVASCRIPTimport { createReduxStore, register } from CE9178">'@wordpress/data'; const DEFAULT_STATE = { isSaving: false, settings: { theme: CE9178">'light', itemsPerPage: 10, }, }; const actions = { setTheme(theme) { return { type: CE9178">'SET_THEME', theme }; }, }; const store = createReduxStore(CE9178">'kb-plugin/settings', { reducer(state = DEFAULT_STATE, action) { switch (action.type) { case CE9178">'SET_THEME': return { ...state, settings: { ...state.settings, theme: action.theme } }; default: return state; } }, selectors: { getTheme(state) { return state.settings.theme; }, }, actions, }); register(store);
We define actions and reducers to ensure state transitions are traceable. Selectors are particularly powerful because they can compute data on the fly, preventing "stale" state issues.
In your React components, you consume this data using the useSelect and useDispatch hooks. This pattern decouples your UI from the underlying data logic.
JAVASCRIPTimport { useSelect, useDispatch } from CE9178">'@wordpress/data'; const ThemeSwitcher = () => { const theme = useSelect((select) => select(CE9178">'kb-plugin/settings').getTheme(), []); const { setTheme } = useDispatch(CE9178">'kb-plugin/settings'); return ( <button onClick={() => setTheme(theme === CE9178">'light' ? CE9178">'dark' : CE9178">'light')}> Current Theme: {theme} </button> ); };
In our Knowledge Base plugin, we often need multiple blocks to react to a single change in settings. By using the global @wordpress/data store, we eliminate the need for complex prop-drilling or event bubbling.
When a user updates a plugin-wide setting, useSelect automatically triggers a re-render in every component subscribed to that selector. This ensures that your UI remains consistent, a critical step toward finalizing dashboard data flow.
store.js in your plugin's assets/src/data directory.kb-plugin/ui that tracks a sidebarOpen boolean state.toggleSidebar action and a isSidebarOpen selector.useDispatch to trigger the toggle and useSelect to render an isOpen class on a wrapper div.{...state}). Never modify the state object directly.apiFetch) inside action creators or via resolvers, not within the reducer.We've moved from local state to global, reactive state management using @wordpress/data. By utilizing custom stores, we've enabled our Knowledge Base plugin to handle complex data synchronization across multiple blocks and admin screens, ensuring a consistent user experience.
Up next: Block API v2 Essentials, where we will define our block's metadata and implement the server-side rendering logic for our Knowledge Base blocks.
Master complex state management by chaining selectors and coordinating cross-component dependencies in your WordPress Data store for consistent, clean UI logic.
Read moreMaster the WordPress Data layer by implementing formal Actions and Reducers. Learn to manage state mutation predictably in your React-based admin dashboard.
State Management with @wordpress/data
Multi-tenancy Considerations
Custom Gutenberg Block Controls
Block Transforms and Deprecation
Dynamic Block Rendering
Advanced State Persistence
Custom Hooks for React