0

I'm working a codebase where it has lots of inconsistency as it has been worked on by multiple different person.

function todos(state = [], action) {
   switch (action.type) {
      case 'ADD_TODO':
      return state.concat([ action.text ])
   default:
      return state
   }
}

Instead of doing concat can I use spread syntax instead?

return [...state, action.text]

Jenny Mok
  • 2,744
  • 9
  • 27
  • 58
  • 1
    You should be able to. Did it break when you tried it or something? – Ry- Jul 10 '17 at 09:05
  • I would use the spread syntax, because it was developed for your needs. It is also the more clean and modern way. – Larce Jul 10 '17 at 09:06
  • @Ryan I'm on mobile, I haven't test it, I want to know the alternative of concat, I want to standardize the code. – Jenny Mok Jul 10 '17 at 09:08
  • @JennyMok: Well, it’d be one spread instead of two, as in `[...state, action.text]`, but I’m not sure why you’d ask this now if you can’t try it anyway. Just wait until you’re off mobile and experiment ¯\\_(ツ)_/¯ – Ry- Jul 10 '17 at 09:09
  • @Ryan action is object right? what is the result of `[...state, action.text]`? – Jenny Mok Jul 10 '17 at 09:11
  • I would choose spread syntax. – kind user Jul 10 '17 at 09:12
  • @JennyMok: The same as the result of `state.concat([action.text])`. – Ry- Jul 10 '17 at 09:12
  • Does this answer your question? [spread operator vs array.concat()](https://stackoverflow.com/questions/48865710/spread-operator-vs-array-concat) – Ramesh Rajendran Jan 04 '20 at 11:38

1 Answers1

1

Both, spread syntax and Array.prototype.concat() dont mutate the state, thus respecting the second rule of redux

State is read-only

taha
  • 997
  • 9
  • 17