我有一个 graphql 查询和 useQuery Hook ,它返回大量数据。我通过单击按钮开始加载,并显示数据加载何时完成。 是否可以使用Promise来等待加载数据?可以写一下吗?
const GET_NAME= gql`
query {
employee {
id
name
}
}
`;
...
function Form () {
...
const { data } = useQuery(
GET_NAME
);
const onClick = () => {
....
return textInput.current!.value=data.employee.name;
最佳答案
使用 getQuery 解构的值
您不需要添加 promise ,这是在内部处理的。
文档中的示例
const { loading, error, data } = useQuery(GET_DOGS);
if (loading) return 'Loading...'; // whilst its loading this will retun
if (error) return `Error! ${error.message}`; // if there's an error this will return
return <pre>{data}</pre> // if the other 2 are not true you must have data, this will return
如您所见,加载状态已经在返回内。以及一个错误。您可以在等待数据返回时响应这些值。
关于javascript - 如何通过 Promise 包装 useQuery hook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60827499/