2016-07-30 25 views
3

我跟着丹·阿布拉莫夫的在https://github.com/tayiorbeii/egghead.io_redux_course_notes/blob/master/08-Reducer_Composition_with_Arrays.md陣營,Redux的傳播運營商在減速返回錯誤「意外的標記」

代碼我收到錯誤消息「在第22行中的異常標記」指的是... TODO 沒認爲這是與巴貝爾預設做......國家工作得很好。當我用map函數替換...... todo時,它會返回相同的錯誤。

///Reducer// 
    export default (state=[], action) => { 
     switch (action.type) { 

     case 'ADD_TODO': 
      return [...state, 
       { 
       id:action.id, 
       text: action.text, 
       completed:false 
       } 
      ]; 

     case 'TOGGLE_TODO': 
      return state.map(todo => { 
      if (todo.id !== action.id) { 
       return todo; 
      } 

      return { 
       ...todo, //returning error 
       completed: !todo.completed 
      }; 
      }); 


     default: 
      return state; 
     } 
    } 

我的調用代碼:

it('handles TOGGLE_TODO',() => { 
    const initialState = [ 
     { 
     id:0, 
     text: 'Learn Redux', 
     completed: false 
     }, 
     { 
     id:1, 
     text: 'Go Shopping', 
     completed: false 
     } 
    ]; 


    const action = { 
     type: 'TOGGLE_TODO', 
     id: 1 
    } 




    const nextstate = reducer(initialState,action) 



    expect (nextstate).to.eql([ 
     { 
     id:0, 
     text: 'Learn Redux', 
     completed: false 
     }, 
     { 
     id:1, 
     text: 'Go Shopping', 
     completed: true 
     } 
    ]) 
+0

傳播經營者被定義爲不同的數組和對象,這就是爲什麼它的工作原理爲...狀態,而不是... todos。你在任何地方都有.babelrc文件嗎?您至少需要第2階段才能使用對象擴展運算符:https://github.com/sebmarkbage/ecmascript-rest-spread –

+0

我沒有.babelrc文件。我現在創建了一個,然後我在.babelrc文件中安裝並添加了一個「transform-object-rest-spread」插件,但是我突然開始收到錯誤消息,即導入是保留字。然後我在.babelrc本身中添加了ES2015預設,現在它工作了。自從......州工作以來,我以爲自己已經有了ES2015的工作。奇怪... –

回答

4

它是關於預設,其實。

陣列蔓延ES2015標準的一部分,你在這裏

 return [...state, 
      { 
      id:action.id, 
      text: action.text, 
      completed:false 
      } 
     ]; 

使用它。然而,這裏

 return { 
      ...todo, //returning error 
      completed: !todo.completed 
     }; 

您使用object spread這不是標準的一部分,但a stage 2 proposal

您需要啓用支持這一提議在巴別塔:https://babeljs.io/docs/plugins/transform-object-rest-spread/或者desugar到Object.assign電話(見this part of the proposal