Search code examples
reactjsreduxreact-reduxpure-function

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
    }
   }
 }
}

Solution

  • 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.