Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何避免阿波罗突变钩的突变破坏?_Reactjs_Typescript_Eslint_React Apollo - Fatal编程技术网

Reactjs 如何避免阿波罗突变钩的突变破坏?

Reactjs 如何避免阿波罗突变钩的突变破坏?,reactjs,typescript,eslint,react-apollo,Reactjs,Typescript,Eslint,React Apollo,我正在包装来自react apollo的usemotation钩子,以便每次使用变异时都能将错误发送到Sentry: import { useMutation } from '@apollo/react-hooks' import { DocumentNode } from 'graphql' import { MutationHookOptions } from 'react-apollo' type UseMutationWithSentryErrorsProps = { refetc

我正在包装来自
react apollo
usemotation
钩子,以便每次使用变异时都能将错误发送到
Sentry

import { useMutation } from '@apollo/react-hooks'
import { DocumentNode } from 'graphql'
import { MutationHookOptions } from 'react-apollo'

type UseMutationWithSentryErrorsProps = {
  refetch: MutationHookOptions<any, Record<string, any>>
  from: string
  mutation: DocumentNode
}

export const useMutationWithSentryErrors = ({
  refetch,
  from,
  mutation
}: UseMutationWithSentryErrorsProps) => {
  const response = useMutation(mutation, refetch)

  const [_apolloMutation, { error: mutationError }] = response
  if (mutationError) {
    captureException(mutationError, {
      info: 'There was an error fetching data.',
      from
    })
  }

  return response
}
这一行的问题在于:

  const [_apolloMutation, { error: mutationError }] = response
我得到一个
变量未读取
错误。如果不访问变异,我无法访问响应错误,但我不是在这个包装函数中使用变异,而是在我真正使用它的地方使用变异。我尝试了经典的
语法,但仍然有相同的错误。

您可以这样做:

const [, { error: mutationError }] = response

注意逗号

我选择它作为额外的检查
if(apolloMutation&&mutationError){等。
const [, { error: mutationError }] = response