{"record":{"id":"2d94fc8ba976faf1","repo":"facebook/docusaurus","slug":"hook-is-called-outside-the-docsidebaritemsexpande","errorCode":null,"errorMessage":"Hook is called outside the <DocSidebarItemsExpandedStateProvider>. ","messagePattern":"Hook is called outside the <DocSidebarItemsExpandedStateProvider>\\. ","errorType":"exception","errorClass":"ReactContextError","httpStatus":null,"severity":"error","filePath":"packages/docusaurus-plugin-content-docs/src/client/docSidebarItemsExpandedState.tsx","lineNumber":52,"sourceCode":" */\nexport function DocSidebarItemsExpandedStateProvider({\n  children,\n}: {\n  children: ReactNode;\n}): ReactNode {\n  const [expandedItem, setExpandedItem] = useState<number | null>(null);\n  const contextValue = useMemo(\n    () => ({expandedItem, setExpandedItem}),\n    [expandedItem],\n  );\n\n  return <Context.Provider value={contextValue}>{children}</Context.Provider>;\n}\n\nexport function useDocSidebarItemsExpandedState(): ContextValue {\n  const value = useContext(Context);\n  if (value === EmptyContext) {\n    throw new ReactContextError('DocSidebarItemsExpandedStateProvider');\n  }\n  return value;\n}\n","sourceCodeStart":34,"sourceCodeEnd":56,"githubUrl":"https://github.com/facebook/docusaurus/blob/3f483e80e326cc646b54b83d564b3f0c4881b9a6/packages/docusaurus-plugin-content-docs/src/client/docSidebarItemsExpandedState.tsx#L34-L56","documentation":"Thrown by useDocSidebarItemsExpandedState() when the context value equals the EmptyContext sentinel, meaning no <DocSidebarItemsExpandedStateProvider> ancestor rendered. This provider tracks which sidebar category index is expanded; it is mounted inside the docs sidebar. ReactContextError produces 'Hook useDocSidebarItemsExpandedState is called outside the <DocSidebarItemsExpandedStateProvider>.'. The sentinel (EmptyContext) rather than null is the missing-provider signal here.","triggerScenarios":"Calling useDocSidebarItemsExpandedState() in a component outside the docs sidebar tree; swizzling the sidebar and forgetting to keep the provider; rendering a sidebar item component in isolation (test/storybook) without the provider.","commonSituations":"Swizzling SidebarItem to add expand/collapse behavior and using the hook at the wrong level; reusing a sidebar subcomponent in a non-sidebar layout; a custom theme that reorganizes the docs layout and drops the provider.","solutions":["Keep useDocSidebarItemsExpandedState() calls within components rendered under <DocSidebarItemsExpandedStateProvider> (i.e. inside the docs sidebar).","If you swizzled the sidebar layout, ensure DocSidebarItemsExpandedStateProvider still wraps the children.","In isolated tests, wrap the component in <DocSidebarItemsExpandedStateProvider>.","Prefer passing expandedItem/setExpandedItem via props if the component is also used outside the sidebar."],"exampleFix":"// before: hook called in a component not under the provider\nfunction CategoryToggle() {\n  const { expandedItem, setExpandedItem } = useDocSidebarItemsExpandedState();\n}\n// after: render within the provider-wrapped sidebar tree\n<DocSidebarItemsExpandedStateProvider>\n  <CategoryToggle />\n</DocSidebarItemsExpandedStateProvider>","handlingStrategy":"validation","validationCode":"// There is no public 'useContext' export for this internal context,\n// so the practical guard is structural: only render the component inside\n// <DocSidebarItemsExpandedStateProvider>.\nfunction useIsInSidebarTree() {\n  // Approximate: the sidebar expanded-state provider only exists under the\n  // docs sidebar layout. Route must be a docs route.\n  const route = useRouteContext();\n  return route?.plugin?.name === 'docusaurus-plugin-content-docs';\n}\n// if (!useIsInSidebarTree()) return null;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep expanded-state hooks inside the docs sidebar subtree.","When swizzling the sidebar, preserve DocSidebarItemsExpandedStateProvider around children.","In tests, wrap components in <DocSidebarItemsExpandedStateProvider>."],"tags":["react-hook","docs","sidebar","react-context","swizzle"],"backgroundTag":null,"analyzedSha":"3f483e80e326cc646b54b83d564b3f0c4881b9a6","analyzedAt":"2026-08-12T13:25:04.382Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}