javascript - 无法读取未定义反应的属性“长度”
问题描述
TypeError: Cannot read property 'length' of undefined
这就是我运行我的反应应用程序时编译器所说的。我需要做什么?
request = (start,end) => {
if(this.state.teams.length < 1){
axios.get(`${ URL }/teams`)
.then( response => {
this.setState({
teams:response.data
})
})
}
axios.get(`${ URL }/articles?_start=${start}&_end=${end}`)
.then( response => {
this.setState({
items:[...this.state.items,...response.data]
})
})
}
解决方案
我建议先检查道具是否未定义或为空甚至已声明。
例如:-
const card = props && props.cards && props.cards.length > 0 ?
props.cards.map((card, i) => {
return (
<card >
)
}) : '';
并退还您的卡。
推荐阅读
- algorithm - 是否有一种算法可以探索网格中的所有单元格,不包括冗余单元格?
- function - 在 Elixir 中,当 lambda 处于闭包中时,如何将 lambda 提取到命名函数?
- php - Laravel Job 立即在浏览器中执行,而不是在后台队列中运行
- c# - 我可以在 C# 的代码编辑器中设置断点吗
- ios - 如何有效地重新排序持久表索引?
- javascript - VScode Javascript 断点被 Chrome 调试器忽略
- android - 以编程方式更改 TextInputLayout 轮廓框颜色
- c++ - OpenGL - 对象轴方向;glm::translate 和 glm::rotate 的顺序
- html - 无法删除表格列之间的空格 (td)
- reactjs - Reactstrap 不渲染