marmelab/react-admin · error · Error
useCreate mutation requires a resource
Error message
useCreate mutation requires a resource
What it means
useCreate's internal mutation function validates that a resource string was provided before calling dataProvider.create. Since the resource normally comes from the hook argument or useResourceContext, calling mutate without a resolvable resource makes creation impossible.
Source
Thrown at packages/ra-core/src/dataProvider/useCreate.ts:112
mutationMode = 'pessimistic',
getMutateWithMiddlewares,
mutationFn: customMutationFn,
onSettled,
...mutationOptions
} = options;
const dataProviderCreate = useEvent((resource: string, params) =>
dataProvider.create<RecordType, ResultRecordType>(
resource,
params as CreateParams<RecordType>
)
);
const customMutationFnWithDataProviderResult = async (
resource: string | undefined,
params: Omit<UseCreateMutateParams<RecordType>, 'resource'>
) => {
if (resource == null) {
throw new Error('useCreate mutation requires a resource');
}
if (params.data == null) {
throw new Error(
'useCreate mutation requires a non-empty data object'
);
}
if (customMutationFn == null) {
return dataProviderCreate(resource, params);
}
return {
data: await customMutationFn({ resource, ...params }),
};
};
const [mutate, mutationResult] = useMutationWithMutationMode<
MutationError,
CreateResult<ResultRecordType>,View on GitHub (pinned to 051f511bb0)
Solutions
- Pass the resource explicitly: mutate('posts', { data: {...} })
- Call useCreate('posts') with a resource argument
- Ensure the component is rendered within a <Resource> / matched route so useResourceContext resolves
Example fix
// before
const { mutate } = useCreate();
mutate({ data: { title: 'Hi' } });
// after
const { mutate } = useCreate('posts');
mutate('posts', { data: { title: 'Hi' } }); Defensive patterns
Strategy: validation
Validate before calling
const resource = resourceFromContext ?? 'posts';
if (resource == null) {
throw new Error('useCreate requires a resource; pass one to the hook or mutate()');
} Type guard
const hasResource = (r: string | undefined | null): r is string => typeof r === 'string' && r.length > 0;
Try / catch
try {
await mutate('posts', { data });
} catch (e) {
if (e.message === 'useCreate mutation requires a resource') {
// fall back to explicit resource or surface a config error
}
throw e;
} Prevention
- Always pass the resource as the first hook argument instead of relying on context
- TypeScript: type the resource parameter as string, not string | undefined
- Render mutation components inside a Resource route or pass resource as a prop
When it happens
Trigger: Calling the mutate/callback returned by useCreate() (e.g. mutate({ data })) when useCreate was called with no resource and no resource is available from the routing/context.
Common situations: Using useCreate() outside of a resource context (not inside a Resource route) and omitting the resource argument; typos in resource naming after refactors.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- useCreate mutation requires a non-empty data object
- useDelete mutation requires a resource
- useDelete mutation requires a non-empty id
- useDeleteMany mutation requires a resource
- useDeleteMany mutation requires an array of ids
AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30).
Data as JSON: /api/errors/c9f8c85fa73f1d8b.
Report an issue: GitHub.