limitlessriver limitlessriver - 1 month ago 16
React JSX Question

Will this be considered a pure reducer function in redux?

I have this reducer function and I am incrementing the value of voteScore by 1 inside it. Is this the right way to do it without breaking the constraint that reducer function should be pure function?

function comment (state = {}, action) {
...
switch(action.type) {
...
case UP_VOTE_COMMENT:
const upVoteScore = parseInt(state[id]['voteScore'], 10) + 1

return {
...state,
[id]: {
...state[id],
voteScore: upVoteScore
}
}
}
}


Also, I have to sort the comments based on the vote score. Will sort inside one of the cases in the reducer be still ok?

Answer Source

Yes. The idea of a pure function is that it always produces the same output based on the input.

The current voteScore is part of the "input" in the parameters.