Reputation: 1362
I'm trying to pass a nested object within an object within a reducer.
im telling redux to pass like
along with the ...post
. However it's giving me
like is not defined.
Reducer
const initialState = {
post: [],
postError: null,
posts:[],
isEditing:false,
isEditingId:null,
likes:0,
postId:null
}
case ADD_LIKE:
console.log(action.id) // renders post id which is 2
console.log(state.posts) // logs posts array
console.log(state.posts)
return {
...state,
posts: state.posts.map(post => {
if (post.id === action.id) {
post.Likes.map( (like) => {
console.log(like); // renders like log
})
}
return {
...post,
...like, /// need to pass the like here but it says is not defined
Likes: like.Likes + 1
}
})
};
console.log(like); // renders this
Like count are being called here
<Like like={id} likes={Likes.length} />
Upvotes: 1
Views: 160
Reputation: 1223
I think you have a problem inside the return.
From the code, Here where you initialize the like
variable (inside the map).
post.Likes.map( (like) => {
console.log(like); // renders like log
})
So, the scope of like will not exist outside of it. But, you are trying to access it outside in return.
return {
...post,
...like,
Likes: like.Likes + 1
}
Edited
If I understand your expectation correct. Here the solution.
return {
...post,
...post.Likes,
Likes: post.Likes.length + 1
}
Upvotes: 1