limitlessriver limitlessriver - 6 months ago 42
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) {
const upVoteScore = parseInt(state[id]['voteScore'], 10) + 1

return {
[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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download