Skip to content

Commit

Permalink
Add missing useCallback to useMutation
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Jun 4, 2019
1 parent 5d64e30 commit 17feeca
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/hooks/useMutation.ts
@@ -1,5 +1,5 @@
import { DocumentNode } from 'graphql';
import { useContext } from 'react';
import { useContext, useCallback } from 'react';
import { pipe, toPromise } from 'wonka';
import { Context } from '../context';
import { OperationResult } from '../types';
Expand Down Expand Up @@ -27,20 +27,23 @@ export const useMutation = <T = any, V = object>(
data: undefined,
});

const executeMutation = (variables?: V) => {
setState({ fetching: true, error: undefined, data: undefined });
const executeMutation = useCallback(
(variables?: V) => {
setState({ fetching: true, error: undefined, data: undefined });

const request = createRequest(query, variables as any);
const request = createRequest(query, variables as any);

return pipe(
client.executeMutation(request),
toPromise
).then(result => {
const { data, error } = result;
setState({ fetching: false, data, error });
return result;
});
};
return pipe(
client.executeMutation(request),
toPromise
).then(result => {
const { data, error } = result;
setState({ fetching: false, data, error });
return result;
});
},
[client, query, setState]
);

return [state, executeMutation];
};

0 comments on commit 17feeca

Please sign in to comment.