facebook/docusaurus · error · Error
Unexpected: cant find current sidebar in context
Error message
Unexpected: cant find current sidebar in context
What it means
Thrown by useCurrentSidebarCategory() when useDocsSidebar() returns null — i.e. the current page has a DocsSidebarProvider but no sidebar is associated with the current doc/route. The function is meant to run only on category index pages, where a sidebar is always present; calling it on a doc page whose sidebar resolved to null, or on a page without a sidebar, triggers this guard. (This is the same guard text reused in useCurrentSidebarSiblings at line 176.)
Source
Thrown at packages/docusaurus-plugin-content-docs/src/client/docsUtils.tsx:119
if (item.type === 'link' && !item.unlisted) {
return item.href;
}
if (item.type === 'category') {
return findFirstSidebarItemCategoryLink(item);
}
// Other items types, like "html"
return undefined;
}
/**
* Gets the category associated with the current location. Should only be used
* on category index pages.
*/
export function useCurrentSidebarCategory(): PropSidebarItemCategory {
const {pathname} = useLocation();
const sidebar = useDocsSidebar();
if (!sidebar) {
throw new Error('Unexpected: cant find current sidebar in context');
}
const categoryBreadcrumbs = getSidebarBreadcrumbs({
sidebarItems: sidebar.items,
pathname,
onlyCategories: true,
});
const deepestCategory = categoryBreadcrumbs.slice(-1)[0];
if (!deepestCategory) {
throw new Error(
`${pathname} is not associated with a category. useCurrentSidebarCategory() should only be used on category index pages.`,
);
}
return deepestCategory;
}
/**
* Gets the category associated with the current location. Should only be used
* on category index pages.View on GitHub (pinned to 3f483e80e3)
Solutions
- Only call useCurrentSidebarCategory() on category index pages (routes that are category landing pages).
- Guard the call: check that useDocsSidebar() returns a non-null sidebar before invoking useCurrentSidebarCategory().
- Verify the doc/route is correctly assigned to a sidebar in the docs sidebar config.
- For shared components, branch on page type (category index vs doc) before calling.
Example fix
// before
function CategoryHeader() {
const category = useCurrentSidebarCategory(); // throws on non-category pages
}
// after: guard the sidebar presence
function CategoryHeader() {
const sidebar = useDocsSidebar();
if (!sidebar) return null;
const category = useCurrentSidebarCategory();
return <h1>{category.label}</h1>;
} Defensive patterns
Strategy: validation
Validate before calling
import {useDocsSidebar} from '@docusaurus/plugin-content-docs/client';
// Restrict useCurrentSidebarCategory() to category index pages:
function useCanUseCurrentSidebarCategory() {
const sidebar = useDocsSidebar();
return sidebar !== null; // provider present + sidebar assigned
}
// if (!useCanUseCurrentSidebarCategory()) return null; Type guard
const hasDocsSidebar = ( sidebar: ReturnType<typeof useDocsSidebar>, ): boolean => sidebar !== null;
Prevention
- Only call useCurrentSidebarCategory() on category index pages.
- Guard with useDocsSidebar() null-check before calling.
- Branch shared components on page type.
When it happens
Trigger: Calling useCurrentSidebarCategory() on a doc page that has no sidebar attached; calling it on a non-doc page; a doc whose sidebar config didn't assign it to any sidebar.
Common situations: Reusing a category-index-only component on a regular doc page; a doc misconfigured so its route doesn't map to a sidebar; swizzling that moves category logic into a shared layout.
Related errors
- ${pathname} is not associated with a category. useCurrentSid
- Sidebar category ${item.label} has neither any subitem nor a
- Hook is called outside the <DocSidebarItemsExpandedStateProv
- Hook is called outside the <DocsSidebarProvider>.
- Invalid sidebars file. The document with id "${docId}" was u
AI-assisted analysis of facebook/docusaurus@3f483e80e3 (2026-08-12).
Data as JSON: /api/errors/3dc43b069c694183.
Report an issue: GitHub.