redux-course-2
redux-course-2 copied to clipboard
Incorrect reduce func implementation for determine current user's vote in Poll component
Hey @tylermcginnis! In the Poll state video you're writing mapStateToProps
for Poll
component, which contains code to figure out answer to the poll of currently authenticated user:
const vote = getVoteKeys().reduce((vote, key) => {
if (vote !== null) {
return vote[0]
}
return poll[key].includes(authedUser)
? key
: vote
}, null)
There's error here and it won't work for case when user responded with option d
. Because it's last element in input array for reduce
and you're just returning key
. You could update it to:
const vote = getVoteKeys().reduce((vote, key) => {
if (vote !== null) {
return vote
}
return poll[key].includes(authedUser)
? key[0] // < --- I've moved indexer to take first letter here
: vote
}, null)
to make it work correctly.
On the published version (in published
branch) there is the same problem with slightly different code.
Thanks! I'll get a fix out soon.