我目前正在尝试在 useEffect Hook 中使用来自 apollo v3 的 useSubscription Hook 更新 GraphQL 订阅:
let containerSubscription = useSubscription<CreatedDateSubscription, CreatedDateSubscriptionVariables>(
gql(containerUpdatedOnCreatedDate),
{
variables: { createdDate: selectedDate },
shouldResubscribe: true, // is this needed?
},
);
// update subscription on date change
React.useEffect(() => {
// how do I update the subscription here?
// setting containerSubscription.variables = ... does not change the subscription
}, [selectedDate]);
我无法在 apollo 文档中找到有关如何解决此问题的任何解决方案。
如有任何帮助,我们将不胜感激!
最佳答案
无需使用 useEffect
——您只需更改 selectedDate
。如果传递给 Hook 的任何选项发生变化,当前订阅将被取消订阅,并使用新选项开始新的订阅。
关于reactjs - 如何更新 useSubscription Hook 中的订阅变量 - Apollo v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60575891/