【发布时间】:2020-03-11 18:14:16
【问题描述】:
import {LOADING_POSTS, GET_ERROR, UPDATE_POSTS} from './postTypes';
const initialState = {
loading : false,
posts : [],
error : ''
};
const postReducer = (state = initialState, action) => {
switch(action.type){
case LOADING_POSTS :
return {
...state,
loading : true
};
case UPDATE_POSTS :
return {
loading : false,
posts : state.posts.concat(action.data),
error : ''
};
case GET_ERROR :
return {
...state,
loading : false,
error : action.error
};
default : return state;
}
}
export default postReducer;
每当我尝试更新帖子时,我都会收到“错误类型错误:无法读取未定义的属性 'concat'”。任何想法可能是什么问题?
【问题讨论】:
-
是的,我做了,什么也没出来...
-
在调用postReducer函数时检查一下,如果你想使用初始状态(因为你有默认值),你应该调用它:postReducer(undefined, action),或者最好把你的可选参数在函数结束时。
-
你可以试试:从参数列表中删除
= initialState,并将其放在你的开关之前的函数顶部 ->if (typeof state === 'undefined') {return initialState} -
@MiroslavGlamuzina 我尝试了你的建议,但我收到一条错误消息,提示未定义 initialState,这解释了为什么 state.posts 也未定义
标签: javascript arrays redux react-redux