marmelab/react-admin · error · Error
useGetRecordId could not find the current record id. You nee
Error message
useGetRecordId could not find the current record id. You need to use it inside a RecordContextProvider, or inside a supported route, or provide the record id to the hook yourself.
What it means
useGetRecordId resolves the current record id from, in order: the explicit recordId argument, the RecordContext, or the :id route param. If all three are absent there is no way to determine the id, so the hook throws with guidance on the three supported sources.
Source
Thrown at packages/ra-core/src/dataProvider/useGetRecordId.ts:22
/**
* Helper hook to get the current `recordId`.
*
* `recordId` is obtained from parameters if passed as a parameter, or from the `RecordContext` if there is one, or, lastly, from the router URL.
*
* @param {any} recordId optional if used inside a RecordContextProvider or if recordId can be guessed from the URL
*
* @returns The `recordId` determined in this manner.
*
* @example
* const recordId = useGetRecordId();
*/
export function useGetRecordId(recordId?: Identifier): Identifier {
const contextRecord = useRecordContext<RaRecord>();
const { id: routeId } = useParams<{ id?: string }>();
const actualRecordId = recordId ?? contextRecord?.id ?? routeId;
if (actualRecordId == null)
throw new Error(
`useGetRecordId could not find the current record id. You need to use it inside a RecordContextProvider, or inside a supported route, or provide the record id to the hook yourself.`
);
return actualRecordId;
}
View on GitHub (pinned to 051f511bb0)
Solutions
- Wrap the component in <RecordContextProvider value={record}>.
- Pass the id explicitly: useGetRecordId(record.id).
- Render the component only on routes with an :id path param (edit/show routes).
Example fix
// before const id = useGetRecordId(); // throws: no context, no route id // after const id = useGetRecordId(record?.id);
Defensive patterns
Strategy: type-guard
Validate before calling
const record = useRecordContext();
const { id: routeId } = useParams<{ id?: string }>();
if (record?.id == null && routeId == null) return <MissingIdFallback />; Type guard
const canResolveId = (r?: { id?: unknown }, routeId?: string): boolean =>
r?.id != null || routeId != null; Try / catch
// hooks cannot be wrapped in try/catch; gate the component instead if (!canResolveId(record, routeId)) return null;
Prevention
- Only call useGetRecordId inside RecordContextProvider or :id routes.
- Pass recordId explicitly when the component is reused outside edit/show pages.
- Keep route param named :id or supply the id manually.
When it happens
Trigger: Calling useGetRecordId() in a component rendered outside any RecordContextProvider; using it on a route without an :id segment (e.g. list or create pages); passing an explicit undefined recordId while no record is in context.
Common situations: Custom edit-page widgets moved to a list page; components rendered in a portal or modal outside the RecordContext; forgetting to pass record prop so RecordContext is empty; routing changes that renamed the :id param.
Related errors
- useCanAccess must be used inside a <Resource> component or p
- useCreateController requires a non-empty resource prop or co
- useEditController requires a non-empty resource prop or cont
- useEditController requires an id prop or a route with an /:i
- useArrayInput must be used inside an ArrayInputContextProvid
AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30).
Data as JSON: /api/errors/c99853521672eada.
Report an issue: GitHub.