问题
Another post shared an example of how to unsubscribe, where the Apollo docs don't. The Apollo docs do mention what subscribeToMore returns...
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.
the question
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();
}, [])
回答1:
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.
You can see the subscription being tracked when subscribeToMore
is called here and then unsubscribed when the query is being cleaned up here.
来源:https://stackoverflow.com/questions/60515463/how-to-unsubscribe-with-usequery-and-subscribetomore