Created
February 14, 2020 12:18
-
-
Save sibelius/a5e233d908928d4ab45e5924bf69df88 to your computer and use it in GitHub Desktop.
useSubscription hook for relay requestSubcription
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GraphQLSubscriptionConfig, requestSubscription } from 'relay-runtime'; | |
import { useRelayEnvironment } from 'react-relay/hooks'; | |
import { useEffect } from 'react'; | |
export const useSubscription = <TSubscriptionPayload extends object>( | |
config: GraphQLSubscriptionConfig<TSubscriptionPayload>, | |
) => { | |
const environment = useRelayEnvironment(); | |
useEffect(() => { | |
const disposable = requestSubscription<TSubscriptionPayload>(environment, config); | |
return () => { | |
disposable.dispose(); | |
}; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment