React-apollo: Debounce tech doesn't work with useQuery

Created on 1 Jun 2019  ·  2Comments  ·  Source: apollographql/react-apollo

Intended outcome:
I tried to implement debounce tech with userQuery on my project and it didn't work.

Actual outcome:
I would like something like this implementation, but with the use of useQuery

const debouncedSearchTerm = useDebounce(offersQueryVariables, 200);
    useEffect(() => {
      (async () => {
        await client.query({ query: getSupportOffers, variables: offersQueryVariables });
      })();
    }, [debouncedSearchTerm]);

How to reproduce the issue:

Version

Most helpful comment

@Jamikk I'm finding myself in a similar situation - what was your solution here?

Hi!
For instance

const SEARCH_DEBOUNCE_TIMEOUT = 200;

const [state, dispatch] = useReducer(reducer, initialState);

// from https://usehooks.com/useDebounce/
const debounceSearch = useDebounce(state.searchValue, SEARCH_DEBOUNCE_TIMEOUT);

const { data, loading, refetch } = useQuery(INQUIRIES_SEARCH, {
    variables: {
      search: debounceSearch,
      page: currentPage,
      count: 10,
      sort: sortByDefault,
    },
    pollInterval: POLL_INTERVAL,
    fetchPolicy: 'network-only',
  });

All 2 comments

@Jamikk I'm finding myself in a similar situation - what was your solution here?

@Jamikk I'm finding myself in a similar situation - what was your solution here?

Hi!
For instance

const SEARCH_DEBOUNCE_TIMEOUT = 200;

const [state, dispatch] = useReducer(reducer, initialState);

// from https://usehooks.com/useDebounce/
const debounceSearch = useDebounce(state.searchValue, SEARCH_DEBOUNCE_TIMEOUT);

const { data, loading, refetch } = useQuery(INQUIRIES_SEARCH, {
    variables: {
      search: debounceSearch,
      page: currentPage,
      count: 10,
      sort: sortByDefault,
    },
    pollInterval: POLL_INTERVAL,
    fetchPolicy: 'network-only',
  });
Was this page helpful?
0 / 5 - 0 ratings