Reputation: 225
I am currently trying to update a GraphQL subscription using the useSubscription hook from apollo v3 in a useEffect hook:
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]);
I could not find any solution in the apollo docs on how to address this problem.
Any help would be appreciated!
Upvotes: 5
Views: 2456
Reputation: 84657
There's no need to use useEffect
-- you just need to change selectedDate
. If any of the options passed to the hook change, the current subscription will be unsubscribed and a new one will be started using the new options.
Upvotes: 6