T
TanStack2y ago
optimistic-gold

Only throw error if there is no data in the cache

Hi everyone, I'm configuring react-query with throwOnError option set to true but I only want the error to be throw when there's no data in the cache. Is there any way to achieve that with react-query ?
3 Replies
deep-jade
deep-jade2y ago
throwOnError: (error, query) => typeof query.state.data === 'undefined'
optimistic-gold
optimistic-goldOP2y ago
work like a charm, thank you. One more question, is it possible to check if there is previous data in the cache, for example a cache with key ['list', {page: 1}] before throwing an error for a query with ['list', {page: 2}] key? My best ideal is to use what you suggested along with placeholderData: keepPreviousData option for certain queries, just not sure if this is the correct approach
deep-jade
deep-jade2y ago
you can take the provided query.queryKey and do fuzzy matching against ['list'] by importing the matchQuery function

Did you find this page helpful?