2017-12-29 37 views
0

작업 생성자가 두 번 호출되는 중, 첫 번째 경우에는 정상적으로 작동하지만 두 번째 호출에서는 예상대로 작동하지 않습니다. 처음에는 입니다. 두 번째 호출에서이 현상이 계속 발생합니다. 내가 뭐하는 거지 나에게 명확하지 않은, 그 흥미로운 정보를 얻을, 그것을 설정하고 전에 콘솔에 상태를 작성"state.set은 함수가 아닙니다."내 반응 - redux - 불변 학습 곡선의 한 부분으로

잘못

이 처음으로 보인다 이러한 설정하기 전에, 상태입니다 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; 
 
    } 
 
}
,369 -이 -이 두 번째 통화에 대한 보인다 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()