Replies: 1 comment 2 replies
-
I think this question highly depends on the implementation of the search component. I think there should be an event like Generally, your flow is absolutely right:
All the other approaches seem more like workarounds that originate from the component not having fine-grained support for different events... |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I have a question on react-query usage that is similar to #1897 and #1549 but my question includes a use case, so perhaps I'll get more specific answers.
Intuitively, I think I'm looking for a way to call react-query imperatively which does not seem to play well with its api, so I'm looking for idiomatic react-query solution to this problem from someone more experienced.
Consider an address-suggestion input that you can use in a form. It can look something like this:
What I want to do is
(1) fetch address results as user types a search query in
(2) have the results cached
(3) show an activity indicator when results are loading (
isLoading
in RQ)A dummy implementation will look something like this
const { data, isLoading } = useQuery('addresses', fetchAddresses)
Now, consider the following user flow:
User opens a page with the form. Problem (P1) is that
fetchAddresses
function will be called right away even though the user has not typed any query. This is easy to fix by addingenabled: searchQuery.lenght > 0
config.Now, as the user types eg. "King's cross", results start showing up. When they see "king's cross, London" they tap on the result and the result gets inserted into the text input.
Problem (P2) that happens now is that because the search query has changed, the
fetchAddresses
api call will fire again, which is unnecessary at this point, and it's also confusing becauseisLoading
will be true and the activity indicator will show up. I just want to update the textinput and not do any api requests at this point.Here's what I tried so far: disable the query and use
refetch
when user types into the textinput (usingonChange
event). This works but the results are not cached.Should I instead force-disable the query when user taps onto a search result? Should I use state or ref to disable the query? Note that I need to re-enable the query as user starts typing again - which means I need to toggle some boolean on and off which I don't quite like, when fundamentally, what I'm looking for is make an api request if and only if user fires a certain action.
So, what is the idiomatic, elegant react-query solution to the problem?
Thank you! 🙂
Beta Was this translation helpful? Give feedback.
All reactions