concat vs spread syntax

2019-08-26 05:35发布

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条回答
我只想做你的唯一
2楼-- · 2019-08-26 06:24

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

State is read-only

查看更多
登录 后发表回答