为什么这段代码彼此不相等?

时间:2019-06-21 13:38:08

标签: javascript arrays redux

被注释的代码等于在三行中编写的相同代码。分开的代码可以正常工作,但是一行编写的代码则行不通。为什么?

let copyArr = [...state];
copyArr.splice(action.index, 1);
return [].concat(copyArr);
//return [].concat([...state].splice(action.index, 1));




// All example of the code is below 

const immutableReducer = (state = [0,1,2,3,4,5], action) => {
  switch(action.type) {
    case 'REMOVE_ITEM': {
      let copyArr = [...state];
          copyArr.splice(action.index, 1)
      // return [].concat(copyArr)
      return [].concat([...state].splice(action.index, 1))
    }
      // don't mutate state here or the tests will fail
    default:
      return state;
  }
};

const removeItem = (index) => {
  return {
    type: 'REMOVE_ITEM',
    index
  }
}

const store = Redux.createStore(immutableReducer);

0 个答案:

没有答案