如何使用useQuery和subscriptionToMore退订 [英] how to unsubscribe with useQuery and subscribeToMore

查看:143
本文介绍了如何使用useQuery和subscriptionToMore退订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一篇帖子分享了一个有关如何退订的示例,其中 Apollo文档不会. Apollo文档确实提到了subscribeToMore返回的内容...

Another post shared an example of how to unsubscribe, where the Apollo docs don't. The Apollo docs do mention what subscribeToMore returns...

subscribeToMore:设置订阅的功能. subscribeToMore返回一个可用于退订的函数.

subscribeToMore: A function that sets up a subscription. subscribeToMore returns a function that you can use to unsubscribe.

这确实给出了提示.来看一个例子会有所帮助.

This does give a hint. It would help to see an example.

useEffect()内使用@apollo/react-hooks并返回subscribeToMore的结果,这是取消订阅组件卸载的方法吗?

Using @apollo/react-hooks, inside of a useEffect() and returning the results of the subscribeToMore, is this the way to unsubscribe on a component unmount?

const { data, error, loading, subscribeToMore } = useQuery(GET_DATA)

useEffect(() => {
    const unsubscribe = subscribeToMore(/*...*/)
    return () => unsubscribe();
}, [])

推荐答案

在卸载组件时,应该为您取消任何关联的订阅.除非您希望在此之前退订,否则不必手动进行管理.

Any associated subscriptions should be unsubscribed for you when the component unmounts. You shouldn't have to manually manage it unless you want to unsubscribe before then.

当调用subscribeToMore时,您可以看到正在跟踪的订阅此处,然后在清理查询时取消订阅

You can see the subscription being tracked when subscribeToMore is called here and then unsubscribed when the query is being cleaned up here.

这篇关于如何使用useQuery和subscriptionToMore退订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆