Skip to content

Instantly share code, notes, and snippets.

@OkancanCosar
Created April 7, 2025 08:32
Show Gist options
  • Select an option

  • Save OkancanCosar/4824a305bbbdb6beadde87a9031532ab to your computer and use it in GitHub Desktop.

Select an option

Save OkancanCosar/4824a305bbbdb6beadde87a9031532ab to your computer and use it in GitHub Desktop.
useMutation cancel signal
import React, { useEffect, useRef, useState } from 'react'
import { View } from 'react-native'
import { useMutation } from '@tanstack/react-query'
export const MyScreen = () => {
const [data, setData] = useState<number[]>([])
const controllerRef = useRef<AbortController | null>(null);
const getListMutation = useMutation({
mutationFn: async () => {
const abortController = new AbortController();
controllerRef.current = abortController;
return myApi.getList(abortController.signal);
},
onSuccess: () => {
setData([1, 2, 3, 4, 5, 6])
},
onError: (err: any) => {
// : {"message": "canceled", "name": "CanceledError"}
// {"message": "Request failed with status code 404", "name": "AxiosError"}
console.log('⚡️ : ', { name: err.name, message: err.message });
},
});
useEffect(() => {
getListMutation.mutate()
return () => {
controllerRef.current?.abort();
}
}, []);
return (
<View style={{}}>
<></>
</View>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment