Skip to main content

usePageState

This hook provides access to the Zustand-based page-level state. Each page in the app has its own isolated state store containing block data, current action, metadata, and any custom state set by blocks. Use a selector function to subscribe to specific slices of state for optimal performance.

Import

import { usePageState } from '@appmaker-xyz/core';

Basic Usage

// Read specific state values using selectors
const blockData = usePageState((state) => state.blockData);
const currentAction = usePageState((state) => state.currentAction);
const isLoading = usePageState((state) => state.blockDataSourceLoading);

// Access custom state set by other blocks
const searchQuery = usePageState((state) => state.searchQuery);

// Get the setter to update page state
const setPageState = usePageState((state) => state.setPageState);
setPageState({ myCustomValue: 'hello' });

// Set a single state variable
const setPageStateVar = usePageState((state) => state.setPageStateVar);
setPageStateVar('searchQuery', 'red shoes');

Parameters

ParameterTypeDescription
selectorFunctionA selector function that receives the full page state and returns the slice you need. E.g., (state) => state.blockData.

Available State Properties

PropertyTypeDescription
blockDataObjectData passed to the page's blocks from the data source.
blockDataSourceLoadingbooleanWhether the block data source is currently loading.
blockDataErroranyError from block data source fetch, if any.
currentActionObjectThe navigation action that opened this page (contains params).
pageIdstringThe page identifier.
metaDataObjectPage metadata.
dataSourceResponseObjectRaw data source responses keyed by name.

Available Actions (via selector)

ActionTypeDescription
setPageStateFunctionMerges an object into page state. Signature: setPageState({ key: value }).
setPageStateVarFunctionSets a single state variable. Signature: setPageStateVar(name, value).
setBlockDataFunctionReplaces the block data and sets loading to false.
updateBlockDataFunctionUpdates a single key in block data. Signature: updateBlockData(key, value).
setCurrentActionFunctionUpdates the current action.
setMetaDataFunctionUpdates the page metadata.
setBlockDataSourceLoadingFunctionSets the loading state.
setBlockDataSourceResponseFunctionStores a data source response. Signature: setBlockDataSourceResponse(name, response).