marmelab/react-admin · error · Error
Cannot generate a link for a record without a resource. You
Error message
Cannot generate a link for a record without a resource. You must use useGetPathForRecord within a ResourceContextProvider, or pass a resource prop.
What it means
useGetPathForRecord computes the URL for a record using the resource context and resource definition hooks. If no resource is available — neither from props/options nor from a ResourceContextProvider (e.g. inside List/Edit) — react-admin throws because a record link cannot be generated without knowing the resource.
Source
Thrown at packages/ra-core/src/routing/useGetPathForRecord.ts:50
* };
*
* // the link option can be a function returning a promise
* const EditLink = ({ record, resource }) => {
* const path = useGetPathForRecord({ record, resource, link: async (record, resource) => {
* const canEdit = await canEditRecord(record, resource);
* return canEdit ? 'edit' : false;
* }});
* return path ? <Link to={path}>Edit</Link> : null;
* };
*/
export const useGetPathForRecord = <RecordType extends RaRecord = RaRecord>(
options: UseGetPathForRecordOptions<RecordType> = {}
): string | false | undefined => {
const { link } = options || {};
const record = useRecordContext(options);
const resource = useResourceContext(options);
if (!resource) {
throw new Error(
'Cannot generate a link for a record without a resource. You must use useGetPathForRecord within a ResourceContextProvider, or pass a resource prop.'
);
}
const resourceDefinition = useResourceDefinition(options);
const createPath = useCreatePath();
const [path, setPath] = useState<string | false>(
link && typeof link !== 'function' && record != null
? createPath({
resource,
id: record.id,
type: link,
})
: false
);
// in preparation for the default value, does the user have access to the show and edit pages?
// (we can't run hooks conditionally, so we need to run them even though the link is specified)
const { canAccess: canAccessShow } = useCanAccess({View on GitHub (pinned to 051f511bb0)
Solutions
- Pass resource explicitly: useGetPathForRecord({ resource: 'posts', record }).
- Render the component inside a ResourceContextProvider (or a List/Edit/Show page that provides one).
- Wrap with <ResourceContextProvider value="posts"> for reusable components used on custom pages.
Example fix
// before
const path = useGetPathForRecord({ record }); // throws on custom page
// after
import { ResourceContextProvider } from 'react-admin';
<ResourceContextProvider value="posts">
<MyLink record={record} />
</ResourceContextProvider>;
// or
const path = useGetPathForRecord({ resource: 'posts', record }); Defensive patterns
Strategy: validation
Validate before calling
const resource = useResourceContext(options);
if (!resource) {
return null; // or render a fallback
}
const path = useGetPathForRecord({ ...options, resource }); Type guard
const hasResource = (r: string | undefined | false): r is string => typeof r === 'string' && r.length > 0;
Try / catch
try {
path = useGetPathForRecord({ record });
} catch (e) {
console.warn('useGetPathForRecord: no resource in context', e);
path = undefined;
} Prevention
- Pass resource explicitly in reusable field/link components.
- Wrap custom-page components with <ResourceContextProvider>.
- Remember portal children don't inherit React context unless still in the React tree — keep them under the provider.
When it happens
Trigger: Calling useGetPathForRecord({ record }) in a component outside any ResourceContextProvider without passing resource in options; rendering such a component directly on a custom page or in a test; options.resource typo'd or undefined at first render.
Common situations: Reusable RecordLink/TextField-derived components moved to a dashboard page (outside <List>/<Edit>); hooks consumed in modals portaled outside the resource tree; forgetting the resource prop on custom field components used outside resource pages.
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
- Cannot generate a link for a record without a resource. You
- useEditController requires an id prop or a route with an /:i
- <InfiniteList> was called outside of a ResourceContext and w
- useListController requires a non-empty resource prop or cont
- useShowController requires a non-empty resource prop or cont
AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30).
Data as JSON: /api/errors/74c3c9e05120b9e7.
Report an issue: GitHub.