You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When dealing with user input, such as in an autocomplete component, it's common to implement debouncing to reduce the number of API calls and improve the user experience.
React Query's useQuery hook makes it easy to manage the state and lifecycle of API requests, and by combining it with debouncing, you can create a powerful and efficient data fetching solution.
In this tutorial, we'll walk through an example of using useQuery with debouncing to fetch country details based on user input in an autocomplete component.
Setting up the custom Hook
Let's start by creating a custom hook called useCountryDetails that encapsulates the logic for fetching country details:
import{useEffect,useState}from'react';import{useQuery}from'@tanstack/react-query';constfetchCountryDetails=async(countryCode,signal)=>{constresponse=awaitfetch(`/api/countries/${countryCode}`,{ signal });returnresponse.json();};exportfunctionuseCountryDetails(){const[countryCode,setCountryCode]=useState('');const[callback,setCallback]=useState(null);constquery=useQuery({queryKey: ['country-details',countryCode],queryFn: async({ signal })=>{returnfetchCountryDetails(countryCode,signal);},enabled: !!countryCode});useEffect(()=>{if(callback){callback(query.isFetching);}},[query.isFetching,callback]);functionfetchCountryDetails(code,cb){setCountryCode(code);setCallback(()=>cb);}return{countryDetails: query.data,
fetchCountryDetails,};}
In this hook:
We define a fetchCountryDetails function that makes an API call to fetch country details based on the provided countryCode.
We use the useQuery hook to manage the API request lifecycle.
The queryKey is set to include the countryCode to ensure that each request is cached separately.
The queryFn uses the fetchCountryDetails function and passes the signal parameter to enable cancellation of the request.
The enabled parameter is set based on the countryCode state, ensuring that the API call is only made when the countryCode is not empty.
We use the useEffect hook to update the loading indicator state whenever the query.isFetching value changes.
The fetchCountryDetails function updates the countryCode state and stores the provided callback function.
The hook returns the countryDetails from the useQuery response and the fetchCountryDetails function.
Since callback is a function, we need to set it properly, as the setState in React has this signature type SetStateAction<S> = S | ((prevState: S) => S);
If you pass a function as initialState, it will be treated as an initializer function. It should be pure, should take no arguments, and should return a value of any type. React will call your initializer function when initializing the component, and store its return value as the initial state
Implementing Debouncing in the Autocomplete Component
Now, let's create an Autocomplete component that uses the useCountryDetails hook and implements debouncing:
We use the useCountryDetails hook to access the fetchCountryDetails function and the fetched countryDetails.
We define a searchTerm state to store the user's input.
We use the useEffect hook to implement debouncing:
We create a delayDebounceFn using setTimeout that will be executed after a 500-millisecond delay.
Inside the delayDebounceFn, we check if the searchTerm is not empty and call the fetchCountryDetails function with the searchTerm.
We pass a callback function to fetchCountryDetails that updates the loading indicator state based on the isFetching value.
We return a cleanup function using clearTimeout to cancel the delayDebounceFn when the component is unmounted or when a new request is initiated.
We render an input field that updates the searchTerm state on change.
If countryDetails is available, we display the country name, capital, and population.
By combining useQuery with debouncing, you can enjoy several benefits:
Reduced API calls: Debouncing ensures that API requests are only made after a short delay, reducing the number of unnecessary calls and improving performance.
Cancellation of requests: The signal parameter in useQuery allows you to cancel requests when they are no longer needed, such as when the component is unmounted or when a new request is initiated.
The text was updated successfully, but these errors were encountered:
When dealing with user input, such as in an autocomplete component, it's common to implement debouncing to reduce the number of API calls and improve the user experience.
React Query's
useQuery
hook makes it easy to manage the state and lifecycle of API requests, and by combining it with debouncing, you can create a powerful and efficient data fetching solution.In this tutorial, we'll walk through an example of using
useQuery
with debouncing to fetch country details based on user input in an autocomplete component.Setting up the custom Hook
Let's start by creating a custom hook called
useCountryDetails
that encapsulates the logic for fetching country details:In this hook:
countryCode
.useQuery
hook to manage the API request lifecycle.queryKey
is set to include thecountryCode
to ensure that each request is cached separately.queryFn
uses the fetchCountryDetails function and passes the signal parameter to enable cancellation of the request.useEffect
hook to update the loading indicator state whenever thequery.isFetching
value changes.fetchCountryDetails
function updates the countryCode state and stores the provided callback function.countryDetails
from the useQuery response and thefetchCountryDetails
function.Since
callback
is a function, we need to set it properly, as thesetState
in React has this signaturetype SetStateAction<S> = S | ((prevState: S) => S);
Read more about useState
Implementing Debouncing in the Autocomplete Component
Now, let's create an Autocomplete component that uses the useCountryDetails hook and implements debouncing:
In the Autocomplete component:
useCountryDetails
hook to access the fetchCountryDetails function and the fetched countryDetails.searchTerm
state to store the user's input.useEffect
hook to implement debouncing:Inside the
delayDebounceFn
, we check if thesearchTerm
is not empty and call thefetchCountryDetails
function with the searchTerm.fetchCountryDetails
that updates the loading indicator state based on the isFetching value.clearTimeout
to cancel thedelayDebounceFn
when the component is unmounted or when a new request is initiated.searchTerm
state on change.countryDetails
is available, we display the country name, capital, and population.By combining
useQuery
with debouncing, you can enjoy several benefits:signal
parameter inuseQuery
allows you to cancel requests when they are no longer needed, such as when the component is unmounted or when a new request is initiated.The text was updated successfully, but these errors were encountered: