Created
August 20, 2019 20:38
-
-
Save cebartling/74c8ab76b3aa7d5cc63d1ea48f8fd276 to your computer and use it in GitHub Desktop.
React component for listening to users GraphQL subscriptions
This file contains 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 React, {useEffect} from 'react'; | |
import {useSubscription} from "@apollo/react-hooks"; | |
import usersSubscription from "../graphql/subscriptions/UsersSubscription"; | |
import {useToasts} from 'react-toast-notifications'; | |
const UsersObserver: React.FC = () => { | |
const {data, loading} = useSubscription(usersSubscription, {}); | |
const {addToast} = useToasts(); | |
useEffect(() => { | |
if (!loading && data) { | |
const message = "Received notification through GraphQL subscription."; | |
console.info(message, data); | |
addToast(message, { | |
appearance: 'success', | |
placement: 'bottom-left', | |
autoDismissTimeout: 4500, | |
autoDismiss: true | |
}); | |
} | |
}, [loading, data, addToast]); | |
return null; | |
}; | |
export default UsersObserver; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment