-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix accidental double network call with useLazyQuery
on some calls of the execute function
#11403
Conversation
🦋 Changeset detectedLatest commit: 95ab853 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
size-limit report 📦
|
7f337a4
to
6e44067
Compare
useLazyQuery
on some calls of the execute function
/release:pr |
A new release has been made for this PR. You can install it with |
Tbh., I'm not 100% sure if I like this fix. For one, I don't 100% agree with the new behavior - but I'll let that aside for now, but it would be good if we discussed that in person. (Adding it to the agenda) What's more important to me is: we call PS: I think the core problem is more that |
After discussing with @phryneas in person and going back through old versions (tested from 3.0 to 3.3), the behavior I've added in this PR differs from historical versions. Previously |
src/react/hooks/useLazyQuery.ts
Outdated
options?.onCompleted?.(...args); | ||
} | ||
); | ||
|
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.
With the type changes that change the type signature of useStableCallback
/useEvent
, this call would look like this.
src/react/hooks/useLazyQuery.ts
Outdated
@@ -147,7 +208,7 @@ export function useLazyQuery< | |||
|
|||
return promise; | |||
}, | |||
[] | |||
[stableOptions, query, initialFetchPolicy] |
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.
What's your take on using an useEvent
here, too, to keep the callback perfectly stable like it was before?
That would also make a lot of the efforts to keep things stable above obsolete.
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.
Sometimes the answer stares you right in the face and you're too busy complicating things to notice...
Love it. Will get this updated! This should hopefully allow me to drop the useEvent
calls for the other callbacks as well 🙂
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.
Co-authored-by: Lenz Weber-Tronic <[email protected]>
This is so much easier to review now ❤️ |
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.
I think my preference would be that we changed only the one runtime statement that is actually related to the fix in this PR, and that we opened a second PR to introduce useEvent
and fixed the other open issue from #11511, as this PR doesn't address all of them in it's current state anyways.
src/react/hooks/useLazyQuery.ts
Outdated
const opts = mergeOptions(options, { | ||
query, |
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.
I have one minor gripe with this PR: It feels like the whole changed code in the PR could be avoided, and we'd have to only change this one single line, without even introducing useEvent
.
I'm fine with this addition if we plan on actually using useEvent
, but if we will only end up using it in this hook, this might be too much noise for what could have been a very small change.
I am having a hard time knowing what version this is in ? |
@billnbell2 this was just released in |
Fixes #9448
Fixes #11201
Fixes an issue where
useLazyQuery
would issue 2 network calls when calling the execute function with no arguments after having called it previously with arguments.This was mainly due to how the callback function used React refs to store values from the execution function that were passed back to
useQuery
in the render function. When callingexecute
without arguments, it would use the previous stored options as the base for merging, which would include variables. Instead, we now properly useoptions
as a dependency to the executeuseCallback
to ensure that it is kept up-to-date with dependencies. With this, we merge passing options from the hook in the execute function instead of relying on the value from a previous ref to get this.