@wordpress/views

In this article

A lightweight package for managing DataViews view state with persistence using WordPress preferences.

The @wordpress/views package provides:

  • Persistence: Automatically saves and restores DataViews state using @wordpress/preferences
  • View Modification Detection: Tracks when views differ from their default state
  • Reset Functionality: Simple reset to default view capability
  • Clean Integration: Drop-in replacement for manual view state management

Installation

Install the module

npm install @wordpress/views --save

API Reference

loadView

Async function for loading view state in route loaders with optional URL parameters.

Usage

// In route loader
const view = await loadView( {
    kind: 'taxonomy',
    name: 'category',
    slug: 'all',
    defaultView,
    queryParams: { page: search.page, search: search.search },
} );

Parameters

  • config ViewConfig: Configuration object for loading the view.
  • config.kind ViewConfig: Entity kind (e.g., ‘postType’, ‘taxonomy’, ‘root’).
  • config.name ViewConfig: Specific entity name.
  • config.slug ViewConfig: View identifier.
  • config.defaultView ViewConfig: Default view configuration.
  • config.queryParams ViewConfig: Object with page and/or search from URL.

Returns

  • Promise resolving to the loaded view object.

useView

Hook for managing DataViews view state with local persistence.

Parameters

  • config ViewConfig: Configuration object for loading the view.
  • config.kind ViewConfig: Entity kind (e.g., ‘postType’, ‘taxonomy’, ‘root’).
  • config.name ViewConfig: Specific entity name.
  • config.slug ViewConfig: View identifier.
  • config.defaultView ViewConfig: Default view configuration.
  • config.queryParams ViewConfig: Object with page and/or search from URL.
  • config.onChangeQueryParams ViewConfig: Optional callback to update URL parameters.

Returns

  • UseViewReturn: Object with current view, modification state, and update functions.