{"record":{"id":"1e2365c486e2a80f","repo":"marmelab/react-admin","slug":"usecreatecontext-must-be-used-inside-a-createconte","errorCode":null,"errorMessage":"useCreateContext must be used inside a CreateContextProvider","messagePattern":"useCreateContext must be used inside a CreateContextProvider","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/ra-core/src/controller/create/useCreateContext.tsx","lineNumber":21,"sourceCode":"import { RaRecord } from '../../types';\nimport { CreateContext } from './CreateContext';\nimport { CreateControllerResult } from './useCreateController';\n\n/**\n * Hook to read the create controller props from the CreateContext.\n *\n * Used within a <CreateContextProvider> (e.g. as a descendent of <Create>).\n *\n * @returns {CreateControllerResult} create controller props\n *\n * @see useCreateController for how it is filled\n */\nexport const useCreateContext = <\n    RecordType extends RaRecord = RaRecord,\n>(): CreateControllerResult<RecordType> => {\n    const context = useContext(CreateContext);\n    if (!context) {\n        throw new Error(\n            'useCreateContext must be used inside a CreateContextProvider'\n        );\n    }\n    return context;\n};\n","sourceCodeStart":3,"sourceCodeEnd":27,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-core/src/controller/create/useCreateContext.tsx#L3-L27","documentation":"useCreateContext reads the CreateContext set by CreateContextProvider (which <Create> mounts automatically). Calling the hook outside that provider means there is no context value, and the hook throws to fail fast rather than return undefined. This indicates a hook/component used outside a Create page.","triggerScenarios":"Calling useCreateContext() or rendering a consumer (e.g. <SimpleForm> variants expecting create context, custom hooks) outside <Create>/CreateContextProvider; testing a component in isolation without the provider wrapper.","commonSituations":"Reusing form components from a Create page in standalone routes without wrapping; unit tests rendering components without CreateContextProvider; refactoring that removed the <Create> wrapper but kept children using the context.","solutions":["Render the component inside <Create resource=\"posts\">, which provides the context","Wrap custom setups with <CreateContextProvider value={controllerProps}>","In tests, wrap the component under test with CreateContextProvider or a <Create> base"],"exampleFix":"// before\nconst MyForm = () => {\n    const { record } = useCreateContext(); // throws: no provider\n    return <span>{record?.id}</span>;\n};\n// after\nconst Page = () => (\n    <Create resource=\"posts\">\n        <MyForm />\n    </Create>\n);\nconst MyForm = () => {\n    const { record } = useCreateContext();\n    return <span>{record?.id}</span>;\n};","handlingStrategy":"type-guard","validationCode":"const insideCreate = typeof useCreateContextSafe === 'function';\n// Or check at runtime with a safe wrapper:\nconst useCreateContextSafe = () => {\n    try { return useCreateContext(); } catch { return null; }\n};","typeGuard":"const isCreateControllerResult = (c: unknown): c is CreateControllerResult =>\n    c != null && typeof c === 'object' && 'resource' in c;","tryCatchPattern":"try {\n    const ctx = useCreateContext();\n    return <div>{JSON.stringify(ctx.record)}</div>;\n} catch (e) {\n    if (e instanceof Error && e.message.includes('must be used inside a CreateContextProvider')) {\n        console.error('Component must be rendered within <Create>');\n        return null;\n    }\n    throw e;\n}","preventionTips":["Only call context hooks in components mounted under their provider (<Create>)","Provide a default value or safe wrapper when components may render standalone","In unit tests, wrap components with the required provider","Keep hooks and their provider-coupled components in the same module for discoverability"],"tags":["react","context","create"],"backgroundTag":"missing-context-provider","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}