Optional
categoryId: stringThe optional ID of the category to fetch and manage.
The returning object contains:
category
: The current category object, or null
if no category is selected or fetched.parents
: An array of parent categories, excluding the root category.fetched
: Boolean indicating whether the category data has been successfully fetched.rename
: CategoryRenameMethod - Function to rename the current category.create
: CategoryCreateMethod - Function to create a new category.delete
: CategoryRenameMethod - Function to delete the current category.useEffect
hook is used to initiate the data fetch whenever
the categoryId
changes.import { useCategory } from 'react-playmakers';
// Using the hook in a component
const CategoryComponent = () => {
const {
category,
parents,
fetched,
create,
rename,
delete: deleteCategory,
} = useCategory("CXXXXXXXX");
useEffect(() => {
if (fetched) {
console.log("Category fetched:", category);
}
}, [fetched]);
// Handle creating a category
const handleCreate = async () => {
await create("New Category", "CXXXXXXXX", "pXXXXXXXX");
};
// Handle renaming the current category
const handleRename = async () => {
await rename("Updated Category Name");
};
// Handle deleting the current category
const handleDelete = async () => {
await deleteCategory();
};
return (
<div>
<h1>{category?.name}</h1>
<button onClick={handleCreate}>Create</button>
<button onClick={handleRename}>Rename</button>
<button onClick={handleDelete}>Delete</button>
</div>
);
};
Generated using TypeDoc
Custom React hook for managing a category.