【问题标题】:Error: React Hook "useLogout" cannot be called inside a callback. Apollo错误:无法在回调中调用 React Hook “useLogout”。阿波罗
【发布时间】:2020-07-07 06:50:46
【问题描述】:

我不确定这个错误是什么以及如何解决它?

代码:

import React, { useContext, useEffect } from 'react'
import { Layout, Loading, LangContext } from '@frontend'
import { gql, useMutation } from '@apollo/client'

const LogoutMutation = gql`
  mutation LogoutMutation {
    logout
  }
`

const Logout = (): JSX.Element => {
  const [useLogout] = useMutation<{ logout: boolean }>(LogoutMutation)

  useEffect(() => {
    useLogout().then(({ data }) => {
      if (data?.logout === true) window.location.href = '/'
    })
  }, [useLogout])

  return null
}

export default Logout

错误:

16:5 错误 React Hook "useLogout" 不能在内部调用 打回来。必须在 React 函数组件中调用 React Hooks 或 一个自定义的 React Hook 函数 react-hooks/rules-of-hooks

【问题讨论】:

    标签: reactjs apollo apollo-client


    【解决方案1】:

    React aslo 识别函数名以 'use' 开头作为自定义钩子,所以我需要更改 useLogout =&gt; doLogout

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2017-11-20
      • 2017-03-03
      • 2021-09-17
      • 1970-01-01
      • 2021-04-03
      • 2021-02-02
      • 2013-02-02
      • 2021-12-28
      相关资源
      最近更新 更多