# useCategory
# Features
useCategory composable is responsible for fetching a list of categories. A common usage scenario for this composable is navigation.
# API
search- a main querying function that is used to query categories and populate thecategoriesobject with the result. Every time you invoke this function API request is made. This method accepts a singleparamsobject. Theparamshas the following options:id: string
search(params?: {id: string}): Promise<void>;
categories: AgnosticCategoryTree[]- a main data object that contains an array of categories fetched bysearchmethod.
interface AgnosticCategoryTree {
label: string;
slug?: string;
items: AgnosticCategoryTree[];
isCurrent: boolean;
count?: number;
[x: string]: unknown;
}
loading: boolean- a reactive object containing information about loading state of yoursearchmethod.
error: UseCategoryErrors- reactive object containing the error message, ifsearchfailed for any reason.
interface UseCategoryErrors {
search: Error;
}
# Getters
getTree- returns category tree.
interface CategoryGetters {
getTree: (category: Category) => AgnosticCategoryTree | null;
}
type Category = {
nodeId: number;
order: number;
name: string;
children: Category[];
metaTitle?: string;
metaKeywords?: string;
metaDescription?: string;
docCount?: number;
url?: string;
};
interface AgnosticCategoryTree {
label: string;
slug?: string;
items: AgnosticCategoryTree[];
isCurrent: boolean;
count?: number;
[x: string]: unknown;
}
# Example
import { useCategory } from '@spryker-vsf/composables';
import { onSSR } from '@vue-storefront/core';
export default {
setup() {
const { categories, search, loading } = useCategory('category-tree');
onSSR(async () => {
await search({});
});
return {
categories,
loading,
};
},
};