{"record":{"id":"3dc43b069c694183","repo":"facebook/docusaurus","slug":"unexpected-cant-find-current-sidebar-in-context","errorCode":null,"errorMessage":"Unexpected: cant find current sidebar in context","messagePattern":"Unexpected: cant find current sidebar in context","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/docusaurus-plugin-content-docs/src/client/docsUtils.tsx","lineNumber":119,"sourceCode":"  if (item.type === 'link' && !item.unlisted) {\n    return item.href;\n  }\n  if (item.type === 'category') {\n    return findFirstSidebarItemCategoryLink(item);\n  }\n  // Other items types, like \"html\"\n  return undefined;\n}\n\n/**\n * Gets the category associated with the current location. Should only be used\n * on category index pages.\n */\nexport function useCurrentSidebarCategory(): PropSidebarItemCategory {\n  const {pathname} = useLocation();\n  const sidebar = useDocsSidebar();\n  if (!sidebar) {\n    throw new Error('Unexpected: cant find current sidebar in context');\n  }\n  const categoryBreadcrumbs = getSidebarBreadcrumbs({\n    sidebarItems: sidebar.items,\n    pathname,\n    onlyCategories: true,\n  });\n  const deepestCategory = categoryBreadcrumbs.slice(-1)[0];\n  if (!deepestCategory) {\n    throw new Error(\n      `${pathname} is not associated with a category. useCurrentSidebarCategory() should only be used on category index pages.`,\n    );\n  }\n  return deepestCategory;\n}\n\n/**\n * Gets the category associated with the current location. Should only be used\n * on category index pages.","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/facebook/docusaurus/blob/3f483e80e326cc646b54b83d564b3f0c4881b9a6/packages/docusaurus-plugin-content-docs/src/client/docsUtils.tsx#L101-L137","documentation":"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.)","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nfunction CategoryHeader() {\n  const category = useCurrentSidebarCategory(); // throws on non-category pages\n}\n// after: guard the sidebar presence\nfunction CategoryHeader() {\n  const sidebar = useDocsSidebar();\n  if (!sidebar) return null;\n  const category = useCurrentSidebarCategory();\n  return <h1>{category.label}</h1>;\n}","handlingStrategy":"validation","validationCode":"import {useDocsSidebar} from '@docusaurus/plugin-content-docs/client';\n\n// Restrict useCurrentSidebarCategory() to category index pages:\nfunction useCanUseCurrentSidebarCategory() {\n  const sidebar = useDocsSidebar();\n  return sidebar !== null; // provider present + sidebar assigned\n}\n// if (!useCanUseCurrentSidebarCategory()) return null;","typeGuard":"const hasDocsSidebar = (\n  sidebar: ReturnType<typeof useDocsSidebar>,\n): boolean => sidebar !== null;","tryCatchPattern":null,"preventionTips":["Only call useCurrentSidebarCategory() on category index pages.","Guard with useDocsSidebar() null-check before calling.","Branch shared components on page type."],"tags":["docs","sidebar","category","react-hook"],"backgroundTag":null,"analyzedSha":"3f483e80e326cc646b54b83d564b3f0c4881b9a6","analyzedAt":"2026-08-12T13:25:04.382Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}