diff --git a/packages/ra-core/src/dataProvider/useGetMany.spec.tsx b/packages/ra-core/src/dataProvider/useGetMany.spec.tsx
index 343c0ebfad1..74b159a3d68 100644
--- a/packages/ra-core/src/dataProvider/useGetMany.spec.tsx
+++ b/packages/ra-core/src/dataProvider/useGetMany.spec.tsx
@@ -40,6 +40,44 @@ describe('useGetMany', () => {
]);
});
+ it('should not call the dataProvider with a GET_MANY on mount if enabled is false', async () => {
+ const dataProvider = {
+ getMany: jest.fn(() =>
+ Promise.resolve({ data: [{ id: 1, title: 'foo' }] })
+ ),
+ };
+ const { dispatch, rerender } = renderWithRedux(
+
+
+
+ );
+ await new Promise(resolve => setTimeout(resolve));
+ expect(dispatch).toBeCalledTimes(0);
+ expect(dataProvider.getMany).toBeCalledTimes(0);
+
+ rerender(
+
+
+
+ );
+ await new Promise(resolve => setTimeout(resolve));
+ expect(dispatch).toBeCalledTimes(5);
+ expect(dispatch.mock.calls[0][0].type).toBe('RA/CRUD_GET_MANY');
+ expect(dataProvider.getMany).toBeCalledTimes(1);
+ expect(dataProvider.getMany.mock.calls[0]).toEqual([
+ 'posts',
+ { ids: [1] },
+ ]);
+ });
+
it('should aggregate multiple queries into a single call', async () => {
const dataProvider = {
getMany: jest.fn(() =>
diff --git a/packages/ra-core/src/dataProvider/useGetMany.ts b/packages/ra-core/src/dataProvider/useGetMany.ts
index 4a2b2e0bc70..3d96ebc8f36 100644
--- a/packages/ra-core/src/dataProvider/useGetMany.ts
+++ b/packages/ra-core/src/dataProvider/useGetMany.ts
@@ -91,7 +91,7 @@ const DataProviderOptions = { action: CRUD_GET_MANY };
const useGetMany = (
resource: string,
ids: Identifier[],
- options: UseGetManyOptions = {}
+ options: UseGetManyOptions = { enabled: true }
): UseGetManyResult => {
// we can't use useQueryWithStore here because we're aggregating queries first
// therefore part of the useQueryWithStore logic will have to be repeated below
@@ -116,6 +116,10 @@ const useGetMany = (
}
dataProvider = useDataProvider(); // not the best way to pass the dataProvider to a function outside the hook, but I couldn't find a better one
useEffect(() => {
+ if (options.enabled === false) {
+ return;
+ }
+
if (!queriesToCall[resource]) {
queriesToCall[resource] = [];
}