Skip to main content
The WordPress data module provides React hooks that enable components to interact with stores. This guide covers the essential patterns for reading data with useSelect and modifying data with useDispatch.

Reading Data with useSelect

The useSelect hook subscribes to store data and re-renders your component when that data changes.

Basic Usage

The useSelect hook takes two arguments:
  1. Callback function: Receives select as first argument, returns derived data
  2. 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: Returns true if resolution has been triggered
  • isResolving: Returns true if resolution is in progress
  • hasFinishedResolution: Returns true if 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:
Warning: Don’t use this pattern in render - your component won’t re-render on data changes.

Writing Data with useDispatch

The useDispatch hook provides access to action creators for modifying state.

Basic Usage

With Dynamic Data

Combine useSelect 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

Use getEntityRecords for collections:
Use 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, use saveEntityRecord:

Error Handling

Save Errors

Check for errors after save operations:

Delete Errors

Best Practices

Cache Benefits

The data module automatically caches responses:
This solves common problems like:
  • 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