concat vs spread syntax

2019-08-26 05:44发布

问题:

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]

回答1:

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

State is read-only