react-query#QueryCache TypeScript Examples

The following examples show how to use react-query#QueryCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Providers.tsx    From vsinder with Apache License 2.0 6 votes vote down vote up
queryCache = new QueryCache({
  defaultConfig: {
    mutations: {
      throwOnError: true,
      onError: (e) => {
        if ("message" in (e as Error)) {
          showMessage({
            message: (e as Error).message,
            duration: 10000,
            type: "danger",
          });
        }
      },
    },
    queries: {
      retry: false,
      staleTime: 60 * 1000 * 5, // 5 mins
      queryFn: defaultQueryFn,
      onError: (e) => {
        if ("message" in (e as Error)) {
          showMessage({
            message: (e as Error).message,
            duration: 10000,
            type: "danger",
          });
        }
      },
    },
  },
})
Example #2
Source File: persist-queries.ts    From nyxo-app with GNU General Public License v3.0 6 votes vote down vote up
readFromStorage = async (key: string): Promise<void> => {
  const storageData = await AsyncStorage.getItem(key)

  if (storageData !== null) {
    const queriesWithData = JSON.parse(storageData)

    Object.keys(queriesWithData).forEach((queryKey) => {
      const data = queriesWithData[queryKey]
      const queryKeyParsed = JSON.parse(queryKey)
      queryCache.setQueryData(queryKeyParsed, data)
    })
  }
}
Example #3
Source File: bookmark-hooks.tsx    From nyxo-website with MIT License 6 votes vote down vote up
useDeleteBookmark = () => {
  return useMutation(removeBookmark, {
    onSuccess: ({ slug }, { type }) =>
      queryCache.setQueryData([type, { slug: slug }], {
        bookmarked: false,
        id: "",
      }),
  })
}
Example #4
Source File: bookmark-hooks.tsx    From nyxo-website with MIT License 6 votes vote down vote up
useAddBookmark = () => {
  return useMutation(addBookmark, {
    onSuccess: ({ slug, id }, { type }) =>
      queryCache.setQueryData([type, { slug: slug }], {
        bookmarked: true,
        id: id,
      }),
  })
}
Example #5
Source File: useCoaching.ts    From nyxo-website with MIT License 6 votes vote down vote up
useUpdateCoaching = () => {
  return useMutation(updateCoaching, {
    onSuccess: (data) => {
      queryCache.invalidateQueries("listCoaching")
      queryCache.setQueryData(["coaching", { id: data?.id }], data)
    },
    onMutate: (updatedCoaching) => {
      queryCache.cancelQueries("listCoaching")
      const previousCoaching = queryCache.getQueryData("listCoaching")

      queryCache.setQueryData("listCoaching", (old) => [
        ...old,
        updatedCoaching,
      ])
      return () => queryCache.setQueryData("listCoaching", previousCoaching)
    },
    onError: (error, updatedCoaching, rollback) => rollback(),

    onSettled: () => {
      queryCache.invalidateQueries("listCoaching")
    },
  })
}
Example #6
Source File: [dataRequestId].tsx    From rcvr-app with GNU Affero General Public License v3.0 5 votes vote down vote up
queryCache = new QueryCache({
  onError: (error) => {
    console.log(error)
  },
})
Example #7
Source File: useCoaching.ts    From nyxo-app with GNU General Public License v3.0 5 votes vote down vote up
useUpdateCoaching = () => {
  return useMutation(updateCoaching, {
    onSuccess: () => {
      queryCache.invalidateQueries('userActiveCoaching')
      queryCache.invalidateQueries('listCoaching')
    }
  })
}
Example #8
Source File: useCoaching.ts    From nyxo-app with GNU General Public License v3.0 5 votes vote down vote up
useDeleteCoaching = () => {
  return useMutation(deleteCoaching, {
    onSuccess: () => {
      queryCache.invalidateQueries('userActiveCoaching')
      queryCache.invalidateQueries('listCoaching')
    }
  })
}