author avatar

anujeet.swain

Mon Nov 04 2024

Query Defaults in React-Query
Any option we pass to React-Query besides the query key can have its default values and can be set by following ways:
• Passing defaultOptions object to query client as global defaults.


const queryClient = new QueryClient(
 defaultOptions: {
  queries: {
   staleTime: 10 * 1000
  }
 }
}


• Setting default options for subset of queries using Fuzzy Matching by setQueryDefaults method


queryClient.setQueryDefaults(
 ['todos','list'],
 {staleTime: 10 * 1000}
) 
//This sets default stale time of 10secs for all the matched queries having keys 'todos' and 'list'


• Setting default options within useQuery for fine grain control over specific query.


useQuery({
 queryKey: ['todo'],
 staleTime: 10 * 1000,
});


Each method takes precedence over the others in this order.
#react-query #customizing-defaults