如何使用 React (Rails) 迭代数组

2024-03-04

我刚刚开始学习 React,我正在尝试找出如何找到我正在寻找的特定值。就像 Ruby 中有each.do 方法并且可以迭代数组一样,我正在尝试使用 React 来做到这一点。

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

I am trying to access the thumbnail._url and when using the debugger, I am not able to access all the objects and images. I thought of this.props.gallery.object.thumbnail_url and other ideas but I am not really sure of the best way! debugger information


Use Array.prototype.map()将数据映射到反应元素。并不是说循环中呈现的元素需要唯一标识符(keys https://facebook.github.io/react/docs/reconciliation.html#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>
    )
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 React (Rails) 迭代数组 的相关文章