简体   繁体   English

React Hooks:使用 useQuery 时挂钩调用无效

[英]React Hooks : Invalid hook call while using useQuery

I have form with a submit button when I submit the form, I'm calling lookup function.当我提交表单时,我有一个带有提交按钮的表单,我正在调用lookup function。 When the code complied to graphQl, It returns Invalid hooks error.当代码符合 graphQl 时,返回 Invalid hooks 错误。

Versions版本

"react": "^17.0.2",
"react-dom": "^17.0.2",

Code:代码:

import React, { useEffect, useState, useCallback } from "react";
import ReactDOM from "react-dom";
import { Button, Form, Input, Row, Card, Col } from "antd";
import { SearchOutlined, LoginOutlined } from "@ant-design/icons";
import { useQuery, useMutation, gql } from "@apollo/client";

const IDP_LOOKUP = gql`
  query getBusiness($name: String!) {
    business(name: $name) {
      id
      name
      domain {
        DomainType
        ... on Domains {
          DomainName
        }
      }
    }
  }
`;

const lookup = (values) => {
  let DomainName = "https://" + values.Orgname + ".com";

  const { data, loading, error } = useQuery(IDP_LOOKUP, {
    variables: {
      name: DomainName
    }
  });
};

const Login = () => {
  return (
    <div>
      <Card size="small" title="Okta Login">
        <Row type="flex">
          <Col lg={24}>
            <Form name="lookup_login" className="login-form" onFinish={lookup}>
              <Row gutter={16} align="center" justify="center" type="flex">
                <Col lg={18}>
                  <Form.Item
                    label="Org Name"
                    name="Orgname"
                    rules={[
                      {
                        required: true,
                        message: "Please enter org name!"
                      }
                    ]}
                  >
                    <Input addonBefore="https://" addonAfter=".com" />
                  </Form.Item>
                </Col>
                <Col lg={6}>
                  <Form.Item>
                    <Button
                      type="primary"
                      htmlType="submit"
                      className="login-form-button"
                    >
                      <SearchOutlined /> Lookup
                    </Button>
                  </Form.Item>
                </Col>
              </Row>
            </Form>
          </Col>
        </Row>
      </Card>
    </div>
  );
};

Error:错误:

Error: Invalid hook call.错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.您可能在同一个应用程序中拥有多个 React 副本,请参阅https://reactjs.org/link/invalid-hook-call以获取有关如何调试和修复此问题的提示。

You shouldn be calling hooks/lookup function inside your Login component, you can declare constant variables outside the function, but not hook.您应该在 Login 组件内调用挂钩/查找 function,您可以在 function 之外声明常量变量,但不能使用挂钩。

const Login = () => {
  const lookup = (values) => {
    let DomainName = "https://" + values.Orgname + ".com";

    const { data, loading, error } = useQuery(IDP_LOOKUP, {
      variables: {
        name: DomainName
     }
  });

 return <></>
};

You get the idea?你明白吗? Though your code is incomplete, for a better explanation, because I can't see your on change handler尽管您的代码不完整,但为了更好的解释,因为我看不到您的更改处理程序

Apollo has a useLazyQuery hook that you can call after the component has rendered. Apollo 有一个useLazyQuery钩子,你可以在组件渲染后调用它。 This returns a tuple where the first item is a function you can then call from your handler to actually perform the query.这将返回一个元组,其中第一项是 function,然后您可以从处理程序调用以实际执行查询。

I think something like this should work:我认为这样的事情应该有效:

const Login = () => {    
  const [lookupIdp, { called, loading, data }] = useLazyQuery(IDP_LOOKUP);

  const lookup = (values) => {
    let DomainName = "https://" + values.Orgname + ".com";

    lookupIdp({
      variables: {
        name: DomainName
      }
    })
  };

  return (
      <div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无效的 Hook 调用 - React Hooks - Invalid Hook Call - React Hooks Gatsby 中的 React 钩子:无效的钩子调用 - React hooks in Gatsby: Invalid hook call 无效的钩子调用 - useContext - React Hooks - Invalid hook call - useContext - React Hooks 错误:无效的挂钩调用。 钩子只能在函数组件的主体内部调用。 使用 React.js 时 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. while using React.js 不使用钩子时无效的钩子调用(material-ui v5) - Invalid hook call while using no hooks (material-ui v5) 使用 Stripe 时对无效的 Hook 调用做出反应 - React Invalid Hook Call While Working With Stripe 添加 React Router DOM 时出错:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - Error While adding React Router DOM: Invalid hook call. Hooks can only be called inside of the body of a function component 错误:Apollo useLazyQuery、useMutation、useQuery 中的无效挂钩调用 - Error: Invalid hook call in Apollo useLazyQuery , useMutation, useQuery 为什么使用查询 function 时会调用“Invalid hook call”? - Why when useQuery function is called “Invalid hook call”? 调用 React 组件中的 useQuery 自定义钩子成功 - Call on Success of useQuery Custom hooks inside React components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM