TanStackT
TanStack2y ago
3 replies
moderate-tomato

How to abort a useMutation ?

Hello,
I have a use case where i have to do several mutations in a promise, but i want to cleanup the queries at unmount.

Now : i am using a signal with an abortController, is there a simpler solution ?

 useEffect(() => {
    const controller = new AbortController()

    const articlePromises = articles.map(({ article }) => {
      return processTask(article.id, controller.signal)
    })

    Promise.allSettled(articlePromises).catch((error) => {
      captureException(error)

      console.error(error)
    })

    return () => {
      controller.abort()
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])



My mutation :

    mutationKey: keysStore.retail.menu.generateBackground(),
    mutationFn: (body) =>
      internalFetcher<GenerateBackgroundRetailMenuOutput, GenerateBackgroundRetailMenuInput>({
        url: '/api/client/multiple',
        body,
        method: 'POST',
        signal: body.signal,
      }),
  })
Was this page helpful?