-
Notifications
You must be signed in to change notification settings - Fork 109
Refetch when resolve prop changes #70
Refetch when resolve prop changes #70
Conversation
Hello @carlesba, thanks for your contribution 👍 I don't really understand your case about the infinity loop and arrow function 🤔 const a = () => "";
const b = a;
a === b; // true Arrow function as any functions (anonymous or named) are compared by reference, so it should works as expected. I will try your branch locally to test this, this is very strange! Also you have forgot the |
I know why the Line 45 in 9064270
So this is working for example (same it("should call the API only 10 times without debounce", async () => {
let apiCalls = 0;
nock("https://my-awesome-api.fake")
.filteringPath(/test=[^&]*/g, "test=XXX")
.get("/?test=XXX")
.reply(200, () => ++apiCalls)
.persist();
const children = jest.fn();
children.mockReturnValue(<div />);
const resolve = a => a;
const { rerender } = render(
<RestfulProvider base="https://my-awesome-api.fake" resolve={resolve}>
<Get path="?test=1">{children}</Get>
</RestfulProvider>,
);
times(10, i =>
rerender(
<RestfulProvider base="https://my-awesome-api.fake" resolve={resolve}>
<Get path={`?test=${i + 1}`}>{children}</Get>
</RestfulProvider>,
),
);
expect(apiCalls).toEqual(10);
}); This is just an issue for our tests, this behaviour should not exists on a real application 😉 We need to check if we can just rerender the |
I forgot about the Provider 😅 I'll change the tests. Thank you! |
Regarding |
@carlesba let's try and maintain that it is identical in behavior to
is that in terms of high-level behavior, it should use the new |
I think this will make it. What do you think? |
@carlesba looks good. I'll take a deeper look later today. Can I ask that you please remove the merge commit from master and instead |
Sure, I'll rebase. |
On tests we can't just rerender Get component but rerender Provider and Get all together. This makes Provider to create a new defaultResolve function on every rerender. To mock a real scenario, resolve function is created in tests and passed as property for Provider so we can keep same the instance on every rerender, which is what it would happen on a real app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
@carlesba THANK YOU for your contribution! You have made our project so much better! |
Why
To provide new set of data when
resolve
prop changes.Related Issue
#63
Issues
resolve
is not matching its previous value when taking the defaultProps (i.e., whenresolve
prop is not defined). However, it works as expected whenresolve
is a defined (non-anonymous) function. That's the reason you can findresolve
being passed in some tests.I've tried to solve it by creating a defined function
defaultResolve
and using it in the default props but it's not working. Help is very welcome here :)Notes
When
resolve
is an anonymous function (what can be very common) we may get an infinite loop asprevProps.resolve
would never be equals tothis.props.resolve
.For this reason I would suggest another solution instead. The component could expose a new prop to handle when the component should refetch. This way it would be more extensible and versatile with no extra code.
Something like:
shouldRefetch = (prevProps: GetProps<TData, TError>, prevState: GetState<TData, TError>) => boolean
The example in the issue #63 could be solved adding an extra component to handle changes in props (
PrevProps
):