如何使用Uselazyquery将其退出加载?

发布于 2025-01-24 19:57:59 字数 1504 浏览 0 评论 0原文

首先,大家好。我正在尝试通过GraphQl改善自己。通常,我可以通过调用usequery来显示我定义的get_characters变量。这里没有问题。但是我想显示与我在输入字段中输入的值相对应的数据。我为此使用了Uselazyquery。但这是问题。我输入文本,然后单击按钮。它一直保持加载。它不会切换到显示。我还研究了其他文章。我还在Uselazyquery中应用了所有提取物,但是结果并没有改变。我还尝试了NotifyOnnetworkStatusChange:True,但它也没有起作用。

感谢您的帮助。提前致谢。

import React, { useEffect, useState } from "react";
import { useQuery, gql, useLazyQuery } from "@apollo/client";
import Card from "./components/Card";

const GET_CHARACTERS = gql`
  query GetCharacterLocations($name: String!) {
    characters(filter: { name: $name }) {
      results {
        id
        name
        image
        location {
          name
        }
      }
    }
  }
`;

const App=()=> {
  const [name, setName] = useState("");
  const [getRickMorty, { loading, error, data, called }] = useLazyQuery(
    GET_CHARACTERS,
    {
      variables: { name },
    }
   );

  console.log({ data, loading, error });

  return (
    <div className="container">
        <input
          value={name}
          onChange={(e) => setName(e.target.value)}
        />

        <button
          onClick={() => {
            getRickMorty();
      }}
        >Search
        </button>

        {loading && <div>Loading</div>}
        {error && <div>Error</div>}

        {data?.characters?.results?.map((characters) => (
          <Card character={characters} />
        ))}
    </div>
  );
}

export default App;

First of all, hello everyone. I'm trying to improve myself with Graphql. Normally, I can display the GET_CHARACTERS variable that I define by calling useQuery. There is no problem here. But I want to display the data that will correspond to the value I entered in the input field. I used useLazyQuery for this. But here is the problem. I enter the text and then click the button. It stays on Loading all the time. It doesn't switch to display. I've also looked at other articles. I've also applied all the fetchPoliciys in useLazyQuery, but the result hasn't changed at all. I also tried notifyOnNetworkStatusChange: true, and it didn't work either.

I would appreciate your help. Thanks in advance.

import React, { useEffect, useState } from "react";
import { useQuery, gql, useLazyQuery } from "@apollo/client";
import Card from "./components/Card";

const GET_CHARACTERS = gql`
  query GetCharacterLocations($name: String!) {
    characters(filter: { name: $name }) {
      results {
        id
        name
        image
        location {
          name
        }
      }
    }
  }
`;

const App=()=> {
  const [name, setName] = useState("");
  const [getRickMorty, { loading, error, data, called }] = useLazyQuery(
    GET_CHARACTERS,
    {
      variables: { name },
    }
   );

  console.log({ data, loading, error });

  return (
    <div className="container">
        <input
          value={name}
          onChange={(e) => setName(e.target.value)}
        />

        <button
          onClick={() => {
            getRickMorty();
      }}
        >Search
        </button>

        {loading && <div>Loading</div>}
        {error && <div>Error</div>}

        {data?.characters?.results?.map((characters) => (
          <Card character={characters} />
        ))}
    </div>
  );
}

export default App;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文