Listing all the blog posts with real-time updates with ReactJS
In this section, we will learn how to use subscriptions in our app to listen to certain events such as post creation, post update, and post delete. As soon as we call the backend through the GraphQL API to perform a task, such as creating a post, the subscribers of the onCreatePost
event will get notified. The same will happen for post update and deletion. Let's get started:
- Add the following code anywhere before the return section of the
App.tsx
file:Â Â const createSubscription: any = API.graphql( Â Â Â Â graphqlOperation(subscriptions.onCreatePost) Â Â ); Â Â createSubscription.subscribe({ Â Â Â Â next: (postData: any) => { Â Â Â Â Â Â console.log("onCreatePost", postData); Â Â Â Â Â Â fetchPosts(); Â Â Â Â }, Â Â }); Â Â const updateSubscription: any = API...