3

I have form with a submit button when I submit the form, I'm calling lookup function. When the code complied to graphQl, It returns Invalid hooks error.

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. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of 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.
4
  • 2
    You shouldn't be calling useQuery from within an event handler. Commented Jul 28, 2022 at 20:21
  • @fynmnx Could you please update the code Commented Jul 28, 2022 at 20:22
  • 1
    Working on an answer... Commented Jul 28, 2022 at 20:24
  • can you share you complete code or github link, to enable us help? Commented Jul 28, 2022 at 20:44

2 Answers 2

1

Apollo has a useLazyQuery hook that you can call after the component has rendered. This returns a tuple where the first item is a function you can then call from your handler to actually perform the query.

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, just noticed that you are also trying to use the hook outside of your component. Updated my answer to show where the handler and hook call should live.
unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering. getting this warning
That sounds like somehow the query is being run prematurely maybe? Perhaps the lookup handler is being called while the component is rendering somehow? I would need to see more I think to help with that.
Are you getting that warning during component render or after submitting the form?
I did not see that warning now. I will keep monitoring. Thank you.
1

You shouldn be calling hooks/lookup function inside your Login component, you can declare constant variables outside the function, but not hook.

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

7 Comments

I'm not sure this is right either. The useQuery should be a statement at the top level of the react component (<Login/>). I think you should place the useQuery there, and use enabled to issue the query when you actually have the data you need to issue it (see here: tanstack.com/query/v4/docs/guides/dependent-queries)
Oh nevermind. I thought this was using react-query, not apollo...
@chukwu3meka After the code change, I'm getting same issue.
can I see where you're handling form value change?
can you share you complete code or github link, to enable us help?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.