javascript - '错误类型错误:无法读取未定义的属性'concat''
问题描述
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'”。任何想法可能是什么问题?
解决方案
似乎state.posts
在您尝试向其添加concat
新数据时未设置。
您可以在那里进行检查以防止错误(如果在posts
设置之前调用它)。如果posts
根本没有设置,请调查为什么它没有通过。
return {
loading : false,
posts : (state.posts
&& Array.isArray(state.posts) // Extra check, you do not need this one if you are certain of the type
&& state.posts.concat(action.data)),
error : ''
};
希望这可以帮助
推荐阅读
- python - 如何在“demo.snmplabs.com”服务器上找到正在运行的进程?
- netlogo - 我必须多次运行模型
- sql - 显示 SQL 跟踪(Springboot+Mybatis)
- jquery - 当子 Div 的显示为无时如何隐藏父 Div
- flutter - Flutter - TextFormField 上的 Time.now()
- python-2.7 - 在 GoogleAppEngine oauth flow 中如何获取 Web App 凭据
- nativescript - 如何在 NativeScript 中设置资产的正确路径?
- dotnetnuke - 禁用 CKEditor 中的“电话号码”字段
- ruby-on-rails - 获取两个日期之间的天数Rails“日期”
- spring-boot - 使用 AutoConfigureMockMvc 和 CompletableFuture 测试 ControllerAdvice