gpt4 book ai didi

javascript - ReactJS:TypeError:无法读取未定义的属性 'map'

转载 作者:搜寻专家 更新时间:2023-11-01 04:12:47 25 4
gpt4 key购买 nike

我在提取位置数据的 ReactJS 应用程序中遇到此错误。在我看来,错误显示在 map() 指向 null

TypeError: Cannot read property 'map' of undefined

我想不出一种方法来设置默认值

这是代码:

import React, {Component} from 'react';
// This component is for search list view, it render the props places data,
// And handle cilck for place item.
class SearchList extends Component {
render() {
// const {places, query, selectPlace} = this.props;
const {places,query,selectPlace} = this.props;


return (

<div className="container">
<hr/>
<div className="input-group">
<input
type="text"
className="form-control"
placeholder="Filter"
aria-label="Filter Input"
onChange={(event) => {
query(event.target.value);
}}
/>
<span className="input-group-addon">
<i className="fas fa-filter"></i>
</span>
</div>
<hr/>
<div style={{maxHeight: '82vh', overflow: 'scroll'}}>
<ul className="list-group">
{
places.map(place => (
<li
tabIndex="0"
key={place.id}
className="list-group-item list-group-item-action"
onClick={() => selectPlace(place)}>
<span>{place.name}</span>
</li>
))
}
</ul>
</div>
</div>
);
}
}

export default SearchList;

最佳答案

可以使用 conditional rendering渲染你的地方。因此,如果 places 未定义,将不会呈现 and 运算符 (&&) 的右操作数

                 <ul className="list-group">
{
places && places.map(place => (
<li
tabIndex="0"
key={place.id}
className="list-group-item list-group-item-action"
onClick={() => selectPlace(place)}>
<span>{place.name}</span>
</li>
))
}
</ul>

关于javascript - ReactJS:TypeError:无法读取未定义的属性 'map',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50845894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com