Upvote
Now we want to add the functionality for the members to upvote the news in the main page. We implement this functionality by using the reactions of the posts.
#
1. Create Upvote ComponentWe use a simple triangle svg to show the upvote logo.
In the component we use useAddReaction
and useRemoveReaction
hooks to add a +1
emoji to the post.
Then we want to know if the user has already reacted to the post or not.
Now if the user has reacted we want to show a green triangle and clicking on it will remove the reaction. Similarly, if the user has not reacted we want to show a grey triangle and clicking on it will add the reaction.
Put them all together and add some styling our Upvote.tsx
will be similar to this.
#
2. Add the Upvote component to the NewsListWe used useFeed
hook in the NewsList. By default, it will only fetch the basic fields of the posts.
Now because we want to get the reactions too, we have to specify it in the fields.
Now we can simply add <Upvote post={post}/>
in the NewsList to have the clickable triangle.
We also add {post.reactionsCount}
to show how many upvotes this post currently have.
After some styling the NewsList.tsx
will be similar to this.
Now by clicking on the grey triangle, you can see that the triangle and number of upvotes immediately changes. This is one of the many powerful features of Tribe. You can only focus on what community you want to build and what features you want to have, we handle the implementation for you.