Home > Net >  Using ReactQuery for Search page, need to call multiple times
Using ReactQuery for Search page, need to call multiple times

Time:01-19

im hoping this is an easy one for an experienced react query user, but i dont see the solution for using ReactQuery as the fetch mechanism for a search page, where the end user updates the search criteria and executes a new search. i see there is a useQueries, but it states its for multiple parallel queries. im looking for:

  • load page without query (no search text)
  • user enters search criteria,
  • execute search query
  • user updates search criteria,
  • execute search query again

CodePudding user response:

You wont't need useQueries for this.

 const {data} = useQuery(['search',searchTerm],()=>/*search api*/,{
  enabled: !!searchTerm
})    
  • searchTerm is passed as queryKey so that whenever it changes a new call to api is made.
  • the query is only enabled when searchTerm is not empty.
  • Related