2017-09-01 10 views
0

私はReactとreduxのチュートリアルに従っていますが、ビデオの最後に同じコードがあるのでエラーが出ます。すでに未知のタイプエラー:state.concatは関数エラーではありません

キャッチされない例外TypeError:state.concatは

すべてのヘルプはそれを感謝される関数ではありません!

//action is a parameter and the reducer is the function 
 
//We will make change to the state based on the action (function) 
 

 
//Function will be reducers 
 
const cards = (state, action) => { 
 
    switch (action.type){ 
 
     case 'ADD_CARD': 
 
      let newCard = Object.assign({}, action.data, { 
 
       score: 1, 
 
       id: +new Date 
 
      }); 
 
//error is here 
 
      return state.concat([newCard]); 
 
     default: 
 
      return state || {}; 
 
    } 
 

 

 
}; 
 

 
//Redux helper function to pass reducer functions 
 
const store = Redux.createStore(Redux.combineReducers({ 
 
    cards: cards 
 
})); 
 

 
//Keep an eye on the store for changes 
 
store.subscribe(() => { 
 
    console.log(store.getState()); 
 
}); 
 

 
//activate store action 
 
store.dispatch({ 
 
    type: 'ADD_CARD', 
 
    data: { 
 
     front: 'front', 
 
     back: 'back' 
 
    } 
 
}); 
 

 
//Another action 
 
store.dispatch({ 
 
    type: 'ADD_CARD', 
 
    data: {} 
 
});

答えて

0

.concatがそれに動作しませんので状態は、オブジェクトではなく、配列でなければなりません。あなたはstate.cards.concat(foobar)のような何かをしなければなりません。私がここで間違っているなら私を訂正してください。しかし、状態を見て、そのタイプを確認してください。

+0

を試みるカードのプロパティであり、それはビデオに著者によるアレイは?ありがとう –

+0

デバッガで確認して確認できますか?コンソールにログを記録して、出力を送ってください。 – stevelacerda7

+0

あなたが話しているチュートリアルと完全なコードを共有できますか? – kevgathuku

0

カードのデフォルトレベルで配列[]の代わりにオブジェクト{}を返していました。

//action is a parameter and the reducer is the function 
 
//We will make change to the state based on the action (function) 
 

 
//Function will be reducers 
 
const cards = (state, action) => { 
 
    switch (action.type){ 
 
     case 'ADD_CARD': 
 
      let newCard = Object.assign({}, action.data, { 
 
       score: 1, 
 
       id: +new Date 
 
      }); 
 
     //splitting the state object into individual properties 
 
      return state.concat([newCard]); 
 
     default: 
 
      return state || []; 
 
    } 
 

 

 
}; 
 

 
//Redux helper function to pass reducer functions 
 
const store = Redux.createStore(Redux.combineReducers({ 
 
    cards: cards 
 
})); 
 

 
//Keep an eye on the store for changes 
 
store.subscribe(() => { 
 
    console.log(store.getState()); 
 
}); 
 

 
//activate store action 
 
store.dispatch({ 
 
    type: 'ADD_CARD', 
 
    data: { 
 
     front: 'front', 
 
     back: 'back' 
 
    } 
 
}); 
 
I wasn't reutning an array at the default in cards instead I had {}. I guest I was returning an object ? 
 

 

 
//Another action 
 
store.dispatch({ 
 
    type: 'ADD_CARD', 
 
    data: {} 
 
});

0

この場合、状態は、このコード

const cards = (state = [], action) => { 
    switch (action.type){ 
     case 'ADD_CARD': 
      let newCard = Object.assign({}, action.data, { 
       score: 1, 
       id: +new Date 
      }); 
      //error is here 
      return state.concat(newCard); 
     default: 
      return state || {}; 
    } 
}; 
関連する問題