javascript - 我想用 .map() 显示我的状态
问题描述
我有这样的状态:
我想在我的渲染中用 .map() 显示它,如下所示:
{this.state.markers.map((marker, key) => (
<Marker
key={key}
coordinate={this.state.markers[marker].coordinate}
title={this.state.markers[marker].title}
description={this.state.markers[marker].address}
image={require('../images/community.png')}
/>
))}
但在每次测试中,我都有这样的回应:
this.state.markers.map is not a function
我怎样才能做到这一点?
解决方案
根据您的ScreenShot,this.state.markers
is not an array
it is an Object
,因此您不能按原样映射它。
为了映射它,您需要使用Object.keys
.
生成的代码将变为
{Object.keys(this.state.markers).map((marker, key) => (
<Marker
key={key}
coordinate={this.state.markers[marker].coordinate}
title={this.state.markers[marker].title}
description={this.state.markers[marker].address}
image={require('../images/community.png')}
/>
))}
推荐阅读
- c++ - 如何定义和设置指向模板类方法的函数指针
- python - ImportError:无法导入名称“moduleTNC”-python
- git - Dockerfile 中的 git install 失败
- css - 仅使用 CSS 向上移动元素
- javascript - Priority-Web-SDK:getRows 限制为 100 个结果
- azure-iot-edge - IoT Edge 模拟 TempSensor 问题
- jquery - 如何从 jQuery 中添加的输入字段中获取值
- python - 在 IE 浏览器的 python 中的 selenium 中设置代理
- sql - SQL:选择一个分类值并排除其他值
- java - 这是进行冒泡排序的正确方法吗?