[英]TypeError: Cannot read properties of undefined (reading 'map') in react JS
TypeError: Cannot read properties of undefined (reading 'map') in react JS TypeError:无法在反应 JS 中读取未定义的属性(读取“地图”)
render() {
return (
<div className="container my-3">
<h2 id="swea">News 24/7 - Top '{this.capitalizeFirstletter(this.props.category)}' Headlines </h2>
{this.state.loading && <Spinner />}
<div className='rows'>
{!this.state.loading && this.state.article.map((element) => {
return <div className="col-md-4" key={element.url}>
<NewsItem title={element.title ? element.title.slice(0, 90) : ""} description={element.description ? element.description.slice(0, 50) : ""} imageUrl={element.urlToImage} newsUrl={element.url} author={element.author} date={element.publishedAt} source={element.source.name} />
</div>
})}
</div>
<div className="container d-flex justify-content-between">
<button type="button" disabled={this.state.page <= 1} className="btn btn-dark" onClick={this.handlePrevClick} >← Previous</button>
<button type="button" disabled={this.state.page + 1 > Math.ceil(this.state.totalResults / this.props.pageSize)} id="xxx" className="btn btn-info" onClick={this.handleNextClick} >Next →</button>
</div>
</div>
);
}
} }
Here's the error *这是错误*
Before mapping over this.state.article first check if the state variable is empty.在映射 this.state.article 之前,首先检查 state 变量是否为空。 For example:
例如:
this.state.article.length?this.state.article.map((item)=>{return item}):null;
Just like cyber said, I was getting through the problem for almost an hour and finally I found the problem is empty variable.就像cyber说的,我解决了这个问题将近一个小时,最后我发现问题是空变量。 I didn't return the result and made my array empty, then I continue get "Cannot read properties of undefined (reading 'map')".Please check your variable.
我没有返回结果并使我的数组为空,然后我继续得到“无法读取未定义的属性(读取'map')”。请检查您的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.