T
TanStack3y ago
deep-jade

Return empty result on empty filter

Hey, what's the best way in tanstack query for following use-case? There's a filter input. Initially the query is disabled and no results are shown. When the user fills the filter input and presses a button, the query should be executed and the results are shown. When the filter input is cleared again (this is the part, where I do not know, how to handle it best), the result should also be cleared. Thanks
3 Replies
fair-rose
fair-rose3y ago
I would put the filter value in a state and use the enabled option of useQuery: - The state is initially undefined - When the user fills out the input and presses the button, the value is stored in the state - When usinguseQuery , pass enabled: !!filter (true when filter has a value, false otherwise) - When the filter is cleared, reset the state back to undefined (it'll disable the query)
xenial-black
xenial-black3y ago
Yeah, this is ideal ☝️
deep-jade
deep-jadeOP3y ago
Thanks this works perfectly!

Did you find this page helpful?