2017-12-29 23 views
0

アクション作成者は2回呼び出されていますが、最初のケースでは正常に動作しますが、2回目の呼び出しでは正常に動作しません。初回は です。 これは、2番目の呼び出しで一貫して発生しています。私は私がやっているものを私に明確ではないこれは、これらの興味深い詳細を取得し、それを設定しています前に、コンソールに状態を書き込む"state.setは関数ではありません"と反応が低下する不変の学習曲線

間違っ

それが初めてと思われるよう、これは設定する前に、状態であり、 enter image description here

// The Action Creator 
 
import config from "../../Config"; 
 
export const getLiveComments =() => { 
 

 
    return (dispatch) => { 
 
     fetchComments() 
 
      .then(response => { 
 
       if (!response.ok){ 
 
        throw Error(response.statusText); 
 
       } 
 

 
       response.json().then(
 
        comments => dispatch(gotComments(comments))) 
 
      } 
 
     ).catch(function(error) { 
 
      console.log('There has been a problem with your fetch operation: ' + error.message); 
 
     }) 
 
    }; 
 

 
}; 
 

 
function fetchComments(){ 
 
    return fetch(`${config.apiurl}/live-comments/get`, { 
 
     method: 'GET', 
 
      credentials: 'same-origin', 
 
      headers: { 
 
       'Accept': 'application/json, text/plain, */*', 
 
       'Content-Type': 'application/json' 
 
      } 
 
     }) 
 
} 
 

 
function gotComments(comments){ 
 
    return { 
 
     type: 'GOT_COMMENTS', 
 
     comments:comments 
 
    } 
 
} 
 

 

 
//The Reducer 
 
import Immutable from 'immutable'; 
 
const initialState = Immutable.fromJS({ 
 
    comments: [] 
 
}); 
 

 
export default function (state = initialState, action) { 
 
    switch (action.type) { 
 
     case ("GOT_COMMENTS"): 
 
      console.log('STATE (COMMENTS)', state) 
 
      return state.set('comments', action.comments).toJS(); 
 
     default: 
 
      return state; 
 
    } 
 
}
- それは - それは2回目の呼び出しのためのようだとして enter image description here

この状態です

reducer-live-comments.js:10 Uncaught (in promise) TypeError: state.set is not a function at ./src/common/ChatMessages/reducer-live-comments.js.webpack_exports.a (reducer-live-comments.js:10) at combineReducers.js:39 at Array.forEach() at combineReducers.js:36 at Map.withMutations (immutable.js:1353) at combineReducers.js:35 at computeNextEntry (:2:27469) at recomputeStates (:2:27769) at :2:31382 at Object.dispatch (createStore.js:165) at dispatch (:2:31875) at index.js:14 at index.js:21 at dispatch (applyMiddleware.js:35) at get-live-comments.js:22 at

答えて

1

問題は、あなたの減速です。 const newState = state.setIn(['comments'], fromJS(action.comments))

詳細情報here

+0

ありがとう:

return state.set('comments', action.comments).toJS();

.toJS()

例えばを削除します。できます!それはリストに追加するためにも機能するのですか、それとも別のものを使うべきですか? – rabashani

1

これは、もはや不変オブジェクトではない.toJS()です。

return state.set('comments', action.comments).toJS(); 

還元剤が次に実行されるとき、状態はバニラオブジェクトになります。

削除.toJS()

関連する問題