【问题标题】:i want to display my state with .map()我想用 .map() 显示我的状态
【发布时间】:2018-04-27 10:06:06
【问题描述】:

我有这样的状态:

我想在我的渲染中用 .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

我该怎么做?

【问题讨论】:

    标签: javascript ios react-native


    【解决方案1】:

    根据您的ScreenShotthis.state.markers 不是array,而是Object因此您无法按原样映射它。 p>

    为了映射它,您需要使用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')}
        />
    ))}
    

    【讨论】:

    • 当然,谢谢。我怎么没想到呢? ^^
    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多