如何使用React(Rails)遍历数组

多数民众赞成

我刚刚开始学习React,我试图弄清楚如何找到我想要的特定值。就像您在Ruby中具有each.do方法,并且可以遍历数组一样,我正在尝试使用React来做到这一点。

class Gallery extends React.Component {
  render () {
    // debugger;
    return (
      <div>
      <img> {this.props.gallery.thumbnail_url} </img>
      </div>
    )
  }
}

我正在尝试访问thumbnail._url,使用调试器时,我无法访问所有对象和图像。我想到了this.props.gallery.object.thumbnail_url和其他想法,但我真的不确定最好的方法!调试器信息

或德罗里

使用Array.prototype.map()的数据映射到反应的元素。并不是在循环中渲染的元素需要唯一的标识符(keys),以使重新渲染列表更具性能。

class Gallery extends React.Component {
  render () {
    const { gallery = [] } = this.props; // destructure the props with a default (not strictly necessary, but more convenient) 

    return (
      <div>
      {
       gallery.map(({ id, thumbnail_url }) => (
         <img key={ id } src={ thumbnail_url } />
       ))
      }
      </div>
    )
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章