TypeError: Cannot read property 'map' of null

这是因为news在this.state = { //... }中初始化时设置为空,后续通过axios获得数据,在遍历数组时要加一个空值判断;

render() {
    const newsList = (this.state.news || []).map((item, index) =>
        <p>{item}</p>
    );
    return(
        <div>
            {newsList}
        </div>
    )
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章