如何使用Uselazyquery将其退出加载?
首先,大家好。我正在尝试通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论