{"record":{"id":"abcc0cc3de2600dd","repo":"marmelab/react-admin","slug":"useeditcontext-must-be-used-inside-an-editcontextp","errorCode":null,"errorMessage":"useEditContext must be used inside an EditContextProvider","messagePattern":"useEditContext must be used inside an EditContextProvider","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/ra-core/src/controller/edit/useEditContext.tsx","lineNumber":22,"sourceCode":"import { EditContext } from './EditContext';\nimport { EditControllerResult } from './useEditController';\n\n/**\n * Hook to read the edit controller props from the EditContext.\n *\n * Used within a <EditContextProvider> (e.g. as a descendent of <Edit>).\n *\n * @returns {EditControllerResult} edit controller props\n *\n * @see useEditController for how it is filled\n */\nexport const useEditContext = <\n    RecordType extends RaRecord = any,\n    ErrorType = Error,\n>(): EditControllerResult<RecordType, ErrorType> => {\n    const context = useContext(EditContext);\n    if (!context) {\n        throw new Error(\n            'useEditContext must be used inside an EditContextProvider'\n        );\n    }\n    return context as EditControllerResult<RecordType, ErrorType>;\n};\n","sourceCodeStart":4,"sourceCodeEnd":28,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-core/src/controller/edit/useEditContext.tsx#L4-L28","documentation":"useEditContext is a hook that reads the EditContext (the controller state of an <Edit> view: record, save, isPending, error, etc.). react-admin throws this error when the hook is called with no EditContextProvider above it in the React tree, because there is nothing to return. It is a guard against using the hook outside an edit page context.","triggerScenarios":"Calling useEditContext() in a component rendered outside <Edit> (e.g. in a custom <List> page, a standalone page, or a storybook render without wrapping the component in <EditContextProvider>), or rendering a component that internally calls the hook without the provider.","commonSituations":"Reusing an edit-page child component (custom toolbar, tab) on a create page or list page; rendering the component in isolation in tests/Storybook; upgrading react-admin where a component previously received props and now relies on context.","solutions":["Wrap the consuming component in <EditContextProvider value={controllerProps}> or render it inside an <Edit> view","If the component must work in multiple views, use the generic useRecordContext/useSaveContext hooks or the useEditController hook instead","In tests/stories, wrap the component with a provider supplying a fake EditControllerResult"],"exampleFix":"// before\nconst MyToolbar = () => {\n  const { record } = useEditContext();\n  ...\n};\n// after\nconst MyToolbar = () => {\n  const { record } = useEditContext();\n  ...\n};\n// usage\n<Edit>\n  <MyToolbar />\n</Edit>\n// or standalone:\nconst controller = useEditController({ resource: 'posts', id: 1 });\n<EditContextProvider value={controller}><MyToolbar /></EditContextProvider>","handlingStrategy":"validation","validationCode":"import { EditContext } from 'ra-core';\nimport { useContext } from 'react';\nconst ensureEditContext = () => {\n  const ctx = useContext(EditContext);\n  if (!ctx) throw new Error('Missing EditContextProvider');\n  return ctx;\n};","typeGuard":"const hasEditContext = (c: unknown): c is EditControllerResult =>\n  c != null && typeof c === 'object' && 'record' in c && 'save' in c;","tryCatchPattern":"// Cannot try/catch hook calls in the same component; guard at a parent level\nconst EditGate = ({ children }) => {\n  const ctx = useContext(EditContext);\n  if (!ctx) return <EditFallback />;\n  return children;\n};","preventionTips":["Mount consumers only under <Edit> / <EditContextProvider>","Use useEditController + provider for custom placements","Provide fake providers in tests/storybook","Prefer context-agnostic hooks (useRecordContext) for reusable children"],"tags":["react-admin","react-context","hooks","edit-view"],"backgroundTag":"hook-used-outside-provider","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}