in issue-tracker/src/useMutation.js [7:48]
export default function useMutation(mutation) {
const environment = useRelayEnvironment();
const [isPending, setPending] = useState(false);
const requestRef = useRef(null);
const mountedRef = useRef(false);
const execute = useCallback(
(config = { variables: {} }) => {
if (requestRef.current != null) {
return;
}
const request = commitMutation(environment, {
...config,
onCompleted: (response) => {
if (!mountedRef.current) {
return;
}
requestRef.current = null;
setPending(false);
config.onCompleted && config.onCompleted(response);
},
onError: error => {
console.error(error);
if (!mountedRef.current) {
return;
}
requestRef.current = null;
setPending(false);
config.onError && config.onError(error);
},
mutation,
});
requestRef.current = request;
setPending(true);
},
[mutation, environment],
);
useEffect(() => {
mountedRef.current = true;
return () => (mountedRef.current = false);
}, []);
return [isPending, execute];
}