last day (15 days later) » 

11:27 AM
0
A: Increase Like count if post id is equal to action.payload

HMRThe following should kind of work but it would be easier to have state.posts as array instead of converting from array to object and object to array every time. To be sure it'll work you need to show the code where you set state.posts case INCREMENT_LIKES_COUNT: return { ...state, lik...

 
Okay, I think I need to give more info regarding this question. I am converting the array into object so I can _id as an index/key. I am getting a list of posts from server and I am displaying them on the screen. Each post has its id and each post has a like button, I want to increment the likeCount on clicking that like button, but when i tried to achieve this, all the post's likeCount was increasing(the behaviour which i obviously do not want). SO, I was asking why I am unable to get the desired behaviour. I hope the question is clear now and I have also updated the question.
 
HMR
@kamal11 The code to increase a like of a post is in the answer (twice). Not sure why you have an object called postDetail and posts on your state but you seem to render the posts in the component. There is no indication what the property indicating likes is on the post item so I just named it likes
 
Your code is doing the same thing as mine. It increases likes count of all the post but I just want to increase the likes count of a particular post. postDetail contains content of the post (eg: post_id, post_author, etc)
 
HMR
@kamal11 You should probably post what your state is with one or two posts and what your post reducer is getting. In your render you do <span>{this.props.likesCount}</span> that should be: <span>{post.likes}</span>
 
<span>{this.props.likesCount}</span> that should be: <span>{post.likes}</span>, this is only true when i am fetching the likes from backend but for now I want to display the likescount from redux state.
 
HMR
11:27 AM
@kamal11: post item likes is (re)set by result[key]= {...value,likes:value.likes+1}; in the first example of my answer and by likes:state.posts[action.payload].likes+1 in the second. I thought state.likesCount is the total number of likes of all posts.
 
This is how the post looks like: { "likesCount": 0,"commentsCount": 1, "likes": [], "comments": [ {"user": {"commentedOn": "2018-10-19T08:44:22.619Z", "avatarPath":"uploads/1539691455295.png", "designation": "ENTTT", "id": "5bc4822309f07600132e525f", "name": "SpartaYesMan"},"_id":"5bc9b6f7c7ef0b0013f28088", "text": "everything is fine", "__v": 0} ], "_id": "5bc72cbaa79914001306d7c8", "postText": "What is happening", "postBy":"KamalYadav","postById":"5bc4822309f07600132e525f",‌​"userDesignation": "Hero", "__v": 1} First I will send the likesCount to backend only then I can grab it from there
 
HMR
You are mixing up the state.likesCount with state.posts[post._id].likesCount.
 
Yes, I know that likesCount is getting reset by the code you provided, but the thing is it is getting reset for all the posts
yes, you are right, I am definitely mixing up, I have to figure out this, maybe I am not getting a hold of this. I think I need some rest :|
 
HMR
That is because you are not using the likesCount of the post item but the likesCount of state.likesCount
Your state looks like {posts:[{_id:1,likesCount:0]}....],likesCount:total?}
`<span>{this.props.likesCount}</span>`
should probably be:
`<span>{post.likesCount}</span>`
My second example of the answer should probably be:

likesCount:state.posts[action.payload].likesCount+1
 
<span>{this.props.likesCount}</span>`
should probably be:
`<span>{post.likesCount}</span>` ---> Totally got your point. Now I understand. Thank you so much for spending so much time on this. I really appreciate it.
 
HMR
11:39 AM
Nice, good luck with your project.
 
Thank you :)
 

last day (15 days later) »