{"record":{"id":"9ffe14dfa052b1d4","repo":"marmelab/react-admin","slug":"usereferencefieldcontext-must-be-used-inside-a-ref","errorCode":null,"errorMessage":"useReferenceFieldContext must be used inside a ReferenceFieldContextProvider","messagePattern":"useReferenceFieldContext must be used inside a ReferenceFieldContextProvider","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/ra-core/src/controller/field/ReferenceFieldContext.tsx","lineNumber":12,"sourceCode":"import { createContext, useContext } from 'react';\nimport type { UseReferenceFieldControllerResult } from './useReferenceFieldController';\n\nexport const ReferenceFieldContext =\n    createContext<UseReferenceFieldControllerResult | null>(null);\n\nexport const ReferenceFieldContextProvider = ReferenceFieldContext.Provider;\n\nexport const useReferenceFieldContext = () => {\n    const context = useContext(ReferenceFieldContext);\n    if (!context) {\n        throw new Error(\n            'useReferenceFieldContext must be used inside a ReferenceFieldContextProvider'\n        );\n    }\n    return context;\n};\n","sourceCodeStart":1,"sourceCodeEnd":18,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-core/src/controller/field/ReferenceFieldContext.tsx#L1-L18","documentation":"useReferenceFieldContext reads the context that <ReferenceField> sets up to share the resolved reference record and link info with its children. The hook throws when no ReferenceFieldContextProvider exists above it, i.e. it is called outside a <ReferenceField> subtree.","triggerScenarios":"Calling useReferenceFieldContext() in a child rendered outside <ReferenceField> (e.g. used in a plain list row, a show view outside any reference field, or a storybook render without ReferenceFieldContextProvider).","commonSituations":"Extracting a child component of ReferenceField into a shared component and reusing it elsewhere; rendering the child in isolation in tests; nesting issues where the component is a sibling rather than a descendant of ReferenceField.","solutions":["Move the consuming component so it is rendered inside <ReferenceField>...</ReferenceField>","Use useRecordContext instead if you only need the current record and not the reference-link context","In tests, wrap with <ReferenceFieldContextProvider value={fakeValue}>"],"exampleFix":"// before\nconst UserName = () => {\n  const { referenceRecord } = useReferenceFieldContext(); // throws outside ReferenceField\n  ...\n};\n<List><UserName /></List>\n// after\n<List>\n  <ReferenceField source=\"user_id\" reference=\"users\">\n    <UserName />\n  </ReferenceField>\n</List>","handlingStrategy":"type-guard","validationCode":"const ctx = useContext(ReferenceFieldContext);\nif (!ctx) return null; // avoid hook misuse downstream","typeGuard":"const isRefFieldCtx = (c: unknown): c is { referenceRecord: RaRecord } =>\n  !!c && typeof c === 'object' && 'referenceRecord' in c;","tryCatchPattern":"function Guard() {\n  const ctx = useContext(ReferenceFieldContext);\n  return ctx ? <UsesHook /> : null;\n}","preventionTips":["Consume inside <ReferenceField> only","Check context before mounting","Prefer useRecordContext when possible","Provider in test setups"],"tags":["react-admin","react-context","hooks","reference-field"],"backgroundTag":"hook-used-outside-provider","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}