useSelect and modifying data with useDispatch.
Reading Data with useSelect
TheuseSelect hook subscribes to store data and re-renders your component when that data changes.
Basic Usage
useSelect hook takes two arguments:
- Callback function: Receives
selectas first argument, returns derived data - Dependencies array: Triggers recalculation when values change
With Query Parameters
Pass query parameters to filter and control API requests:Always include values used inside the callback in the dependencies array. This ensures the selector re-runs when those values change.
Multiple Selectors
Return an object to use multiple selectors:Resolution Status
Track whether data is still loading using resolution selectors:hasStartedResolution: Returnstrueif resolution has been triggeredisResolving: Returnstrueif resolution is in progresshasFinishedResolution: Returnstrueif resolution completed
Always pass the exact same arguments to
hasFinishedResolution as you pass to the selector. Store them in a variable to avoid typos.Getting Selectors Directly
For event callbacks where you don’t need reactivity, get the selectors function:Writing Data with useDispatch
TheuseDispatch hook provides access to action creators for modifying state.
Basic Usage
With Dynamic Data
CombineuseSelect and useDispatch for actions based on current state:
Using Registry Select in Dispatch
Access dynamic data at dispatch time using the registry:Working with Entity Records
Fetching Entity Records
UsegetEntityRecords for collections:
getEntityRecord for single records:
Entity Records vs Edited Entity Records
The data module distinguishes between:- Entity Records: Data as fetched from the API (no local edits)
- Edited Entity Records: Data with local edits applied
Edited entity records store only the raw value as a string, not the rendered HTML. This is because JavaScript cannot properly render server-side dynamic content like shortcodes.
Editing Entity Records
Saving Entity Records
Creating New Records
For new records without a pageId, usesaveEntityRecord:
Error Handling
Save Errors
Check for errors after save operations:Delete Errors
Best Practices
Cache Benefits
The data module automatically caches responses:- Out-of-order API responses
- Duplicate requests
- Stale data
Consistent Selector Arguments
Ensure selector arguments match exactly:Async Action Patterns
Actions return promises - use async/await:Next Steps
- Entities & Undo/Redo - Learn about entity architecture
- Building Forms - Create complete data-driven forms