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...