@wordpress/views 是一个轻量级包,用于通过 WordPress 偏好设置管理 DataViews 视图状态并实现持久化。它提供自动保存和恢复、视图修改检测、重置功能,并作为手动状态管理的即插即用替代方案。
// 使用 loadView 异步加载视图状态
const view = await loadView({
kind: 'postType',
name: 'post',
slug: 'list',
defaultView: { layout: 'grid' },
activeViewOverrides: { sort: 'date' },
queryParams: { page: 1, search: 'example' }
});
// 使用 useView 钩子管理视图状态
const { view, isModified, updateView } = useView(config);
// 使用 useViewConfig 钩子获取实体视图配置
const config = useViewConfig({ kind: 'taxonomy', name: 'category' });A lightweight package for managing DataViews view state with persistence using WordPress preferences.
The @wordpress/views package provides:
@wordpress/preferencesInstall the module
npm install @wordpress/views --save
Async function for loading view state in route loaders.
Parameters
ViewConfig: Configuration object for loading the view.ViewConfig: Entity kind (e.g., ‘postType’, ‘taxonomy’, ‘root’).ViewConfig: Specific entity name.ViewConfig: View identifier.ViewConfig: Default view configuration.ViewConfig: View overrides applied on top but never persisted.ViewConfig: Object with page and/or search from URL.Returns
Hook for managing DataViews view state with local persistence.
Parameters
ViewConfig: Configuration object for loading the view.Returns
UseViewReturn: Object with current view, modification state, and update functions.A hook that retrieves the view configuration for a given entity from the core data store.
Parameters
Object:string: The kind of the entity.string: The name of the entity.Returns
Object: An object containing the default_view, default_layouts, and view_list configuration for the entity.