Apollo Client:访问自定义钩子内的钩子函数调用变量?

时间:2021-04-08 03:50:44

标签: reactjs react-hooks apollo-client

我正在尝试创建一个自定义的 Apollo 突变挂钩,我需要在其中访问挂钩外部的变量:

const useCustomMutation = () => {

  return useMutation(
    GRAPHQL_QUERY,
    {
      update: (store, response) => {
             store.writeQuery({
               query: A_DIFFERENT_QUERY,
               data: externalVariable // <= This is were I need access (simplified code).
             });          
      },
    }
  );
}

在另一个文件中,我会像这样调用钩子:

const someVariable = 1234;

const [mutationFunction, {data, loading, error}] = useCustomMutation();

mutationFunction({ variables: { queryVariable: someVariable }}); // this is the variable I need to access above

如何在无需将参数传递给钩子本身的情况下访问自定义钩子内的 queryVariable/someVariableexternalVariable 所在的位置)?

// I'm trying to avoid this: 
// I want to access mutationFunction variables, not pass a variable to the hook.
const someVariable = 1234;
const [mutationFunction] = useCustomMutation(someVariable);

0 个答案:

没有答案