gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:42:00 32 4
gpt4 key购买 nike

我正在浏览 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 useQueryhook, 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 associatedquery.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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com