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.)

Solutions

  1. Only call useCurrentSidebarCategory() on category index pages (routes that are category landing pages).
  2. Guard the call: check that useDocsSidebar() returns a non-null sidebar before invoking useCurrentSidebarCategory().
  3. Verify the doc/route is correctly assigned to a sidebar in the docs sidebar config.
  4. 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

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


AI-assisted analysis of facebook/docusaurus@3f483e80e3 (2026-08-12). Data as JSON: /api/errors/3dc43b069c694183. Report an issue: GitHub.

Appendix: 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)