Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Fix sending request by Query with deep props on every update #3176

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions packages/ra-core/src/util/Query.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,15 @@ describe('Query', () => {

it('should not dispatch a new fetch action when updating with the same query props', () => {
let dispatchSpy;
const myPayload = {};
const { rerender } = render(
<TestContext>
{({ store }) => {
dispatchSpy = jest.spyOn(store, 'dispatch');
const myPayload = {
foo: {
bar: 1,
},
};
return (
<Query
type="mytype"
Expand All @@ -232,15 +236,22 @@ describe('Query', () => {
);
rerender(
<TestContext>
{() => (
<Query
type="mytype"
resource="myresource"
payload={myPayload}
>
{() => <div>Hello</div>}
</Query>
)}
{() => {
const myPayload = {
foo: {
bar: 1,
},
};
return (
<Query
type="mytype"
resource="myresource"
payload={myPayload}
>
{() => <div>Hello</div>}
</Query>
);
}}
</TestContext>
);
expect(dispatchSpy.mock.calls.length).toEqual(1);
Expand Down
6 changes: 3 additions & 3 deletions packages/ra-core/src/util/Query.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, ReactNode } from 'react';
import { shallowEqual } from 'recompose';
import equal from 'deep-equal';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deep-equal isn't a direct dependency. use lodash/isEqual instead

import withDataProvider from './withDataProvider';

type DataProviderCallback = (
Expand Down Expand Up @@ -107,8 +107,8 @@ class Query extends Component<Props, State> {
if (
prevProps.type !== this.props.type ||
prevProps.resource !== this.props.resource ||
!shallowEqual(prevProps.payload, this.props.payload) ||
!shallowEqual(prevProps.options, this.props.options)
!equal(prevProps.payload, this.props.payload) ||
!equal(prevProps.options, this.props.options)
) {
this.callDataProvider();
}
Expand Down