import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
useCategoriesEnabled
A hook that checks whether the categories feature is enabled in the application settings.
Source file: template/hooks/use-categories-enabled.ts
Overview
useCategoriesEnabled reads the categoriesEnabled flag from the SettingsProvider context. Because the value is server-rendered and passed through React context, there is no loading delay or network request -- the result is available immediately on mount.
This hook answers the question "is the categories feature turned on?" which is controlled by the application configuration. It does not check whether any categories actually exist in the database (see useCategoriesExists for that).
Signature
function useCategoriesEnabled(): {
categoriesEnabled: boolean;
loading: boolean;
error: Error | null;
}
Parameters
This hook takes no parameters.
Return Value
| Property | Type | Description |
|---|---|---|
categoriesEnabled | boolean | true if the categories feature is enabled in application settings, false otherwise |
loading | boolean | Always false -- the value is available synchronously from context |
error | Error | null | Always null -- no asynchronous operation is performed |
Fallback Behavior
If the component is rendered outside of a SettingsProvider, the hook falls back to categoriesEnabled: true (the provider's default fallback value).
Implementation Details
- The hook calls
useSettings()to access theSettingsProvidercontext. - It destructures the
categoriesEnabledboolean from the context value. - It returns the value along with
loading: falseanderror: null, since the value comes from server-rendered context and involves no asynchronous fetching.
Usage Examples
Conditionally rendering a categories section
import { useCategoriesEnabled } from '@/hooks/use-categories-enabled';
function Sidebar() {
const { categoriesEnabled } = useCategoriesEnabled();
return (
<aside>
<SearchWidget />
{categoriesEnabled && <CategoriesWidget />}
<RecentItems />
</aside>
);
}
Guarding a categories page
import { useCategoriesEnabled } from '@/hooks/use-categories-enabled';
import { redirect } from 'next/navigation';
function CategoriesPage() {
const { categoriesEnabled } = useCategoriesEnabled();
if (!categoriesEnabled) {
redirect('/');
}
return <CategoriesList />;
}
Combined with useCategoriesExists
import { useCategoriesEnabled } from '@/hooks/use-categories-enabled';
import { useCategoriesExists } from '@/hooks/use-categories-exists';
function CategoriesFilter() {
const { categoriesEnabled } = useCategoriesEnabled();
const { data } = useCategoriesExists();
// Only show filter when feature is enabled AND categories exist
if (!categoriesEnabled || !data?.exists) {
return null;
}
return <CategoryFilterDropdown />;
}
Requirements
| Dependency | Purpose |
|---|---|
SettingsProvider | Provides the categoriesEnabled value through React context |
Related Hooks
useCategoriesExists-- Checks whether categories exist in the databaseuseCompaniesEnabled-- Checks whether the companies feature is enableduseTagsEnabled-- Checks whether the tags feature is enableduseSurveysEnabled-- Checks whether the surveys feature is enabled