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

Add possibility to manually unsubscribe from useLazyQuery #2055 #2133

Closed
wants to merge 2 commits into from

Conversation

damianborowy
Copy link

Related to issue #2055

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 17, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1a40475:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
reduxjs/redux-toolkit Configuration
@examples-action-listener/counter Configuration

@netlify
Copy link

netlify bot commented Mar 17, 2022

✅ Deploy Preview for redux-starter-kit-docs ready!

🔨 Explore the source changes: 1a40475

🔍 Inspect the deploy log: https://app.netlify.com/sites/redux-starter-kit-docs/deploys/6234eb2c12d4cf0008f84be1

😎 Browse the preview: https://deploy-preview-2133--redux-starter-kit-docs.netlify.app

Comment on lines 845 to 848
const info = useMemo(() => ({ lastArg: arg }), [arg])
return useMemo(
() => [trigger, queryStateResults, info],
[trigger, queryStateResults, info]
() => [trigger, queryStateResults, info, unsubscribe],
[trigger, queryStateResults, info, unsubscribe]
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to make unsubscribe part of the info object and maybe give it a better name. Open to suggestions.

Copy link
Author

Choose a reason for hiding this comment

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

That's some good advice, I missed the fact that info was already an object for some extra things so I can touch it without breaking anything 😄 not sure if extras is the best possible name though, waiting for further feedback!

@phryneas
Copy link
Member

phryneas commented Jul 3, 2022

We just did a grooming of PRs and discussed this: would you be okay to to a PR for a reset method instead that unsubscribed and set the query hook back to isUninitialized?

It feels inconsistent to us that after unsubscribing, the query state will still stay in the component for another 60 seconds - or even forever, if another component is still using it.

Or do you have a good use case that would only make sense with unsubscribe, but not with reset?

@henkkasoft
Copy link

henkkasoft commented Nov 2, 2022

We just did a grooming of PRs and discussed this: would you be okay to to a PR for a reset method instead that unsubscribed and set the query hook back to isUninitialized?

It feels inconsistent to us that after unsubscribing, the query state will still stay in the component for another 60 seconds - or even forever, if another component is still using it.

Or do you have a good use case that would only make sense with unsubscribe, but not with reset?

This is exactly what I am looking for I think.

I am using useLazyQuery with query params given by user.
The API response is HTTP ERROR 422 when the user input is incorrectly formed.
UI will show modal of the error and then I want to recover without unmounting the component. So I would want to get rid of the isError state and the user could modify the input and trigger the query again.

Is this possible somehow already?
And if not, did I understood correctly that this PR (when unsubscribe is extended as reset) will enable this kind of usage?

@phryneas
Copy link
Member

@henkkasoft it will not remove the value, just unsubscribe from it. It will be removed after 60 second if no other component uses it.

Maybe you are giving isError too much importance here?
As it sounds "there is an error from the server" will 100% be true until the data is sent to the server without a 422 - so maybe you just need an additional local state to control that message?

@markerikson markerikson added this to the 2.x bugfixes milestone Dec 6, 2023
@stchristian
Copy link

Hi, I see that there was no activity on this PR for a while now. Is this still pending? I would be happy to take over this PR and implement the above mentioned reset method
@damianborowy @henkkasoft

@markerikson
Copy link
Collaborator

@stchristian yeah, no one has looked at this in a year. Might be best to re-create it and file a new PR with the updates

@stchristian
Copy link

@markerikson Before starting this feature, I would suggest adding a skip parameter instead of reset method, similar to useQuery() that would work the same way setting the hook to uninitialized. This way the query stays consistent with the existing interface.

@henkkasoft
Copy link

@henkkasoft it will not remove the value, just unsubscribe from it. It will be removed after 60 second if no other component uses it.

Maybe you are giving isError too much importance here? As it sounds "there is an error from the server" will 100% be true until the data is sent to the server without a 422 - so maybe you just need an additional local state to control that message?

I solved my problem earlier so that after showing input-error modal I stored startedTimeStamp so that unless it it changed the modal is not shown again. It worked nice even the reseting was not possible... until now.

My guess is that the encountered problem now is this:
Because reseting is not done the query (even in the isError state) is invalidated when somewhere in the app doing some modification invalidation of correct tag is done. Invalidation change the startedTimeStamp of the query and the input-error modal is shown again.

It is really confusing for the user that some old input error from advanced search is shown again when doing some modification elsewhere in the app. In our app the advanced search can be open in the app and still other tasks can be performed. So it is not uncommon to try to search something but fail the search and continue usage so that the latest advanced search is left open and that is why the reseting or atleast unsubscribing this lazy query would be important to be able to perform.

Any updates on this PR / issue #2055 ?

@henkkasoft
Copy link

I was able to prevent invalidation of the query in isError state by adding error check to providesTags function:
return error ? [] : ['MyTag']
Now error state queries are not invalidated when 'MyTag' is invalidated in some mutation's invalidatesTags function.

Not sure if this is still the best solution...

@markerikson
Copy link
Collaborator

Replaced by #4689

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants