{"record":{"id":"77b1f4d7d0593429","repo":"facebook/docusaurus","slug":"hook-is-called-outside-the-docspreferredversionco","errorCode":null,"errorMessage":"Hook is called outside the <DocsPreferredVersionContextProvider>. ","messagePattern":"Hook is called outside the <DocsPreferredVersionContextProvider>\\. ","errorType":"exception","errorClass":"ReactContextError","httpStatus":null,"severity":"error","filePath":"packages/docusaurus-plugin-content-docs/src/client/docsPreferredVersion.tsx","lineNumber":187,"sourceCode":" * This is a maybe-layer. If the docs plugin is not enabled, this provider is a\n * simple pass-through.\n */\nexport function DocsPreferredVersionContextProvider({\n  children,\n}: {\n  children: ReactNode;\n}): ReactNode {\n  return (\n    <DocsPreferredVersionContextProviderUnsafe>\n      {children}\n    </DocsPreferredVersionContextProviderUnsafe>\n  );\n}\n\nfunction useDocsPreferredVersionContext(): ContextValue {\n  const value = useContext(Context);\n  if (!value) {\n    throw new ReactContextError('DocsPreferredVersionContextProvider');\n  }\n  return value;\n}\n\n/**\n * Returns a read-write interface to a plugin's preferred version. The\n * \"preferred version\" is defined as the last version that the user visited.\n * For example, if a user is using v3, even when v4 is later published, the user\n * would still be browsing v3 docs when she opens the website next time. Note,\n * the `preferredVersion` attribute will always be `null` before mount.\n */\nexport function useDocsPreferredVersion(\n  pluginId: string | undefined = DEFAULT_PLUGIN_ID,\n): {\n  preferredVersion: GlobalVersion | null;\n  savePreferredVersionName: (versionName: string) => void;\n} {\n  const docsData = useDocsData(pluginId);","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/facebook/docusaurus/blob/3f483e80e326cc646b54b83d564b3f0c4881b9a6/packages/docusaurus-plugin-content-docs/src/client/docsPreferredVersion.tsx#L169-L205","documentation":"Thrown by useDocsPreferredVersionContext() when the context value is falsy, i.e. no <DocsPreferredVersionContextProvider> ancestor. This provider (the safe wrapper DocsPreferredVersionContextProvider around the unsafe variant) persists the user's last-visited version per plugin id. ReactContextError yields 'Hook useDocsPreferredVersionContext is called outside the <DocsPreferredVersionContextProvider>.'. Note the guard here is `if (!value)` rather than a sentinel.","triggerScenarios":"Calling useDocsPreferredVersion() outside the provider tree; swizzling the root layout and dropping DocsPreferredVersionContextProvider; rendering a version-aware component on a page not under the provider.","commonSituations":"Customizing the root Theme/Layout and removing the provider that Docusaurus wires into it; reusing a version-switcher component in a context where the provider was never mounted; partial swizzle that kept the unsafe provider but removed the safe wrapper.","solutions":["Ensure <DocsPreferredVersionContextProvider> remains in the root layout (Docusaurus wires it via the theme's providers; keep it when swizzling Root).","Only call useDocsPreferredVersion() under that provider subtree.","In tests, wrap renders in DocsPreferredVersionContextProvider.","If the component is used outside versioned contexts, gate the call behind a route/version check."],"exampleFix":"// before: swizzled Root dropped the provider\nconst Root = ({children}) => <>{children}</>;\n// after: keep the provider in the provider chain\nconst Root = ({children}) => (\n  <DocsPreferredVersionContextProvider>\n    {children}\n  </DocsPreferredVersionContextProvider>\n);","handlingStrategy":"validation","validationCode":"import {useContext} from 'react';\nimport {DocsPreferredVersionContext} from '@docusaurus/plugin-content-docs/client';\n\nfunction useHasPreferredVersionProvider() {\n  return useContext(DocsPreferredVersionContext) != null;\n}\n// (export the context or use the provider's presence structurally)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never remove DocsPreferredVersionContextProvider from the swizzled Root layout.","Only call useDocsPreferredVersion() on doc/versioned routes.","In tests, wrap renders in DocsPreferredVersionContextProvider."],"tags":["react-hook","docs","versioning","react-context","swizzle"],"backgroundTag":null,"analyzedSha":"3f483e80e326cc646b54b83d564b3f0c4881b9a6","analyzedAt":"2026-08-12T13:25:04.382Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}