{"record":{"id":"dfc215e8877b8582","repo":"facebook/docusaurus","slug":"hook-is-called-outside-the-docsversionprovider","errorCode":null,"errorMessage":"Hook is called outside the <DocsVersionProvider>. ","messagePattern":"Hook is called outside the <DocsVersionProvider>\\. ","errorType":"exception","errorClass":"ReactContextError","httpStatus":null,"severity":"error","filePath":"packages/docusaurus-plugin-content-docs/src/client/docsVersion.tsx","lineNumber":33,"sourceCode":" * Provide the current version's metadata to your children.\n */\nexport function DocsVersionProvider({\n  children,\n  version,\n}: {\n  children: ReactNode;\n  version: PropVersionMetadata | null;\n}): ReactNode {\n  return <Context.Provider value={version}>{children}</Context.Provider>;\n}\n\n/**\n * Gets the version metadata of the current doc page.\n */\nexport function useDocsVersion(): PropVersionMetadata {\n  const version = useContext(Context);\n  if (version === null) {\n    throw new ReactContextError('DocsVersionProvider');\n  }\n  return version;\n}\n","sourceCodeStart":15,"sourceCodeEnd":37,"githubUrl":"https://github.com/facebook/docusaurus/blob/3f483e80e326cc646b54b83d564b3f0c4881b9a6/packages/docusaurus-plugin-content-docs/src/client/docsVersion.tsx#L15-L37","documentation":"Thrown by useDocsVersion() when DocsVersionContext holds null, meaning no <DocsVersionProvider> ancestor rendered with a non-null version. The provider is set up by the docs theme around doc pages to supply the active version metadata; calling useDocsVersion() outside a doc route (navbar, layout, homepage) throws. ReactContextError yields 'Hook useDocsVersion is called outside the <DocsVersionProvider>.'. Note the provider accepts version: PropVersionMetadata | null, but the hook treats null as the error condition.","triggerScenarios":"Calling useDocsVersion() in a layout/navbar/footer rendered on non-doc pages; using a version-aware component on the homepage or a blog page; swizzling the docs layout and removing DocsVersionProvider.","commonSituations":"Reusing a version badge / version-aware component in a global layout; a swizzle of DocPage that dropped the provider; importing a docs-only component into a custom page.","solutions":["Only call useDocsVersion() within the docs page subtree (under DocsVersionProvider).","For layout components that must work on all pages, use useDocsVersionCandidates() or route-aware hooks that degrade gracefully outside docs.","Re-add DocsVersionProvider in any swizzled docs layout.","In tests, wrap renders in <DocsVersionProvider version={mockVersion}>."],"exampleFix":"// before\nfunction VersionBadge() {\n  const v = useDocsVersion(); // throws on non-doc pages\n  return <span>{v.label}</span>;\n}\n// after: guard by route type, or use a layout-safe hook\nfunction VersionBadge() {\n  const route = useRouteContext();\n  if (route?.plugin?.name !== 'docusaurus-plugin-content-docs') return null;\n  const v = useDocsVersion();\n  return <span>{v.label}</span>;\n}","handlingStrategy":"validation","validationCode":"import useRouteContext from '@docusaurus/useRouteContext';\n\nfunction useIsDocRoute() {\n  return useRouteContext()?.plugin?.name === 'docusaurus-plugin-content-docs';\n}\n// if (!useIsDocRoute()) return null; // before calling useDocsVersion()","typeGuard":"const isDocRoute = (\n  route: ReturnType<typeof useRouteContext>,\n): boolean => route?.plugin?.name === 'docusaurus-plugin-content-docs';","tryCatchPattern":null,"preventionTips":["Only call useDocsVersion() within DocsVersionProvider (doc pages).","For layout components, use route-aware hooks (useDocsVersionCandidates) instead.","Keep DocsVersionProvider in swizzled DocPage; wrap tests with it."],"tags":["react-hook","docs","versioning","react-context","swizzle"],"backgroundTag":null,"analyzedSha":"3f483e80e326cc646b54b83d564b3f0c4881b9a6","analyzedAt":"2026-08-12T13:25:04.382Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}