useAppState
Description
A React hook that provides access to the global application state. Returns the current application context including GraphQL response data, configuration, props map, widgets, and fetching state.
Import
import { useAppState } from '@components/common/context/app';
Usage
import { useAppState } from '@components/common/context/app';
function ProductList() {
const { graphqlResponse, config, fetching } = useAppState();
const products = graphqlResponse.products || [];
if (fetching) {
return <div>Loading...</div>;
}
return (
<div>
{products.map(product => (
<ProductCard key={product.id} product={product} />
))}
</div>
);
}
Return Value
Returns an AppStateContextValue object with the following properties:
| Property | Type | Description |
|---|---|---|
graphqlResponse | Record<string, any> | The GraphQL response data for the current page. |
config | Config | Application configuration including page meta, tax settings, and catalog settings. |
propsMap | Record<string, any[]> | Map of component props organized by component ID. |
widgets | any[] | Array of widget configurations (optional). |
fetching | boolean | Whether page data is currently being fetched. |
Config Type
interface Config {
pageMeta: PageMetaInfo;
tax: {
priceIncludingTax: boolean;
};
catalog: {
imageDimensions: { width: number; height: number };
};
}
Example: Using Configuration
import { useAppState } from '@components/common/context/app';
function ProductPrice({ price }) {
const { config } = useAppState();
const priceIncludesTax = config.tax.priceIncludingTax;
return (
<div className="product-price">
<span className="amount">${price}</span>
<span className="tax-info">
{priceIncludesTax ? '(incl. tax)' : '(excl. tax)'}
</span>
</div>
);
}
Related
- AppProvider - The provider component that wraps the application
- useAppDispatch - Hook to access methods for updating the state