我正在浏览 Apollo React hooks 的文档。
并且看到有两个查询钩子(Hook)可用于 useQuery
和 useLazyQuery
我正在阅读这一页。
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? TheuseLazyQuery
hook is perfect for executing queries in response to events other than component rendering. This hook acts just likeuseQuery
, with one key exception: whenuseLazyQuery
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/