작업 생성자가 두 번 호출되는 중, 첫 번째 경우에는 정상적으로 작동하지만 두 번째 호출에서는 예상대로 작동하지 않습니다. 처음에는 입니다. 두 번째 호출에서이 현상이 계속 발생합니다. 내가 뭐하는 거지 나에게 명확하지 않은, 그 흥미로운 정보를 얻을, 그것을 설정하고 전에 콘솔에 상태를 작성"state.set은 함수가 아닙니다."내 반응 - redux - 불변 학습 곡선의 한 부분으로
잘못
// 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;
}
}
![enter image description here](https://i.stack.imgur.com/JTSq8.png)
이 상태입니다
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
감사 :
return state.set('comments', action.comments).toJS();
.toJS()
예컨대을 제거합니다. 그것은 작동합니다! 목록에 추가 할 때도 사용할 수 있습니까? 아니면 다른 것을 사용해야합니까? – rabashani