reactjs - Apollo graphQL 中的 useQuery 和 useLazyQuery 有什么区别?

标签 reactjs graphql apollo

我正在浏览 Apollo React hooks 的文档。
并且看到有两个查询钩子(Hook)可用于 useQueryuseLazyQuery我正在阅读这一页。
https://www.apollographql.com/docs/react/api/react/hooks/
有人可以解释一下它们之间有什么区别,在什么情况下应该使用它。

最佳答案

useQuery由组件调用,它随后触发查询。
但是当useLazyQuery被调用后,不会触发后续的查询,而是返回一个可用于手动触发查询的函数。
在此页面上进行了说明:
https://www.apollographql.com/docs/react/data/queries/#manual-execution-with-uselazyquery

When React mounts and renders a component that calls the useQuery hook, Apollo Client automatically executes the specified query. But what if you want to execute a query in response to a different event, such as a user clicking a button? The useLazyQuery hook is perfect for executing queries in response to events other than component rendering. This hook acts just like useQuery, with one key exception: when useLazyQuery is called, it does not immediately execute its associated query. Instead, it returns a function in its result tuple that you can call whenever you're ready to execute the query.

关于reactjs - Apollo graphQL 中的 useQuery 和 useLazyQuery 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63681650/

相关文章:

javascript - 如何使用 HTML 类属性渲染 React 组件?

javascript - React 只为点击的元素提供动画

javascript - GraphQL 能否在解析器中选择性地解析给定查询结果的字段?

graphql - 如何在前端代码中使用 GraphQL 枚举(例如在 <select> 中)?

javascript - CSS-in-JS 不会阻止浏览器进行 CSS 缓存吗?

javascript - 尝试删除时,React 列表会删除错误的项目

graphql - GraphQL 解析器参数的错误顺序(根、参数、上下文)

java - graphql-java - 如何在 spring boot 中使用订阅?

python - 如何在 Graphql 突变中返回 PDF 文件?

ios - 嵌套查询 GraphQL : Syntax error - iOS