React-query: Cannot debounce API calls

Created on 27 Mar 2020  路  11Comments  路  Source: tannerlinsley/react-query

I am trying to use this for a search component, like:

  const { status, data, error, isFetching } = useQuery(
    searchQuery && ['results', searchQuery],
    getSearchAsync,
  );

I would like the call to getSearchAsyncto be debounced while the user is typing the query. Tried adding lodash.debounce like:

  const { status, data, error, isFetching } = useQuery(
    searchQuery && ['results', searchQuery],
    debounce(getSearchAsync, 3000),
  );

But since this is a function component it is not working as expected. The getSearchAsyncmethod is only called multple times after the timeout expires, which I expect to be once with the latest value (searchQuery).

Most helpful comment

I'd like to see this, since react-query is serializing the key it's doing all the heavy lifting there, and could then debounce the result of the key (which devs have no hook into that result).

Can this be reopened?

All 11 comments

You need to use a debounce that supports promises. It needs to return a promise immediately, then debounce off of that promise.

@tannerlinsley thanks for the reply. Can you give an example?

Wow...there is no debounce package that does this?
Or even better...can't react-query have an option for this?

You may try to debounce searchQuery change instead, something like that.

@denisborovikov thanks...that works nicely!
Still...wouldn't it be nice to have this as an option in react-query?

I'd like to see this, since react-query is serializing the key it's doing all the heavy lifting there, and could then debounce the result of the key (which devs have no hook into that result).

Can this be reopened?

@tannerlinsley could this be reconsidered? Debouncing based on the serialized query key would be an elegant solution, and in my view a reasonable responsibility for react-query. I'm interested to try my hand in making a PR if it has any chance of getting merged.

For me, if I type very fast I cannot see the input. Only after I finish typing I see the text. After that, all works fine.
Here is a my code:

const [searchTerm, setSearchTerm] = useState('')
const debouncedSearchTerm = useDebounce(searchTerm, 500)
const { data } = useSearch({ searchTerm: debouncedSearchTerm })

@tbntdima If you use searchTerm as your input's value prop you should see as you type. You probably use debouncedSearchTerm instead and that's why the input is denounced. Debounced value should be used for the query only.

@denisborovikov the problem is a bit different. When searchTerm changes, the component was re-rendering anyway or at least tried to. And because of this, I was getting lags. My eventual solution was to move the denounce login into the upper component 馃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ivowork picture ivowork  路  5Comments

oukayuka picture oukayuka  路  4Comments

benediktviebahn picture benediktviebahn  路  4Comments

john-pc picture john-pc  路  6Comments

alveshelio picture alveshelio  路  6Comments