react + apollo/client 实现中断接口请求
import { useQuery } from '@apollo/client';
import { useState } from 'react';
const ExampleComponent = () => {
const [controller, setController] = useState(null);
const { data } = useQuery(MY_QUERY, {
context: {
fetchOptions: {
signal: controller?.signal
}
}
});
const handleAbort = () => {
if (controller) {
controller.abort();
}
};
return (
<>
<button onClick={() => setController(new AbortController())}>发起请求</button>
<button onClick={handleAbort}>中断请求</button>
</>
);
};