简体   繁体   English

如何从对象数组中获取精确的 id?

[英]How to get a precise id from the array of objects?

Making an application (react + redux).制作应用程序(react + redux)。 It contains a few photos.它包含几张照片。 When you click a photo you get a modal window with bigger photo and comments.当你点击一张照片时,你会得到一个带有更大照片和评论的模态 window。

App component is:应用组件是:

class App extends Component {
  componentDidMount() {
    const { getPhotos } = this.props
    getPhotos()
  }

  getPhoto = (id) => {
    const { openModal } = this.props
    openModal(id)
  }

  render() {
    const { photos, isOpen, isFetching } = this.props
    return (
      <div className="container">
        <Header />
        <div className="list__content">
          {isFetching ? (
            <p>Loading...</p>
          ) : (
            photos.map((photo) => (
              <List
                key={photo.id}
                src={photo.url}
                id={photo.id}
                onClick={this.getPhoto}
              />
            ))
          )}
        </div>
        <Footer />
        {isOpen && <ModalContainer />}
      </div>
    )
  }
}

export default connect(
  ({ photos, modal }) => ({
    photos: photos.photos,
    isFetching: photos.isFetching,
    isOpen: modal.isOpen,
  }),
  { getPhotos, openModal }
)(App)

List component:列表组件:

const List = ({ src, onClick }) => {
  return (
    <div className="list__item">
      <img src={src} alt={src} onClick={onClick} />
    </div>
  )
}

export default List

You can see the prop onClick={this.getPhoto} in the List component.您可以在 List 组件中看到道具onClick={this.getPhoto} If I change it like this onClick={this.getPhoto(photo.id)} it won't work correctly because it gets id's of all photos and shows them all (one by one) in my modal window.如果我像这样onClick={this.getPhoto(photo.id)}更改它,它将无法正常工作,因为它会获取所有照片的 id 并在我的模态 window 中(一张一张)显示它们。

So, how to get a precise id of the photo in that case with my method getPhoto ?那么,在这种情况下,如何使用我的方法getPhoto获取照片的精确 ID?

set onClick to function that call getPhoto将调用 getPhoto 的 onClick 设置为getPhoto

photos.map((photo) => (
  <List
    key={photo.id}
    src={photo.url}
    id={photo.id}
    onClick={() => this.getPhoto(photo.id)}
  />
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从具有ID的数组中获取多个对象? - How to get multiple objects from an array with an id? 如何从对象数组中获取 id 值? - How to get id value from array of objects? 如何通过 ID 字段从存储在 Vuex 中的对象数组中获取项目? - How to get an item from an array of objects stored in Vuex by ID field? 试图从带有 cypress 的对象数组中获取 id - Trying to get an id from an array of objects with cypress 如何从具有特定 id 的数组中获取对象与另一个数组编号进行比较 - How can I get objects from array with specific id compares from another array numbers 如何从一系列月份对象[{month:&#39;Jan&#39;id:&#39;1},{month:&#39;Feb&#39;,id:&#39;2},…{month:&#39;Dec&#39;,id :12}]? - How to get next 4 months from current month in an array of month objects [{month:'Jan' id:'1},{month:'Feb', id:'2},…{month:'Dec', id:12}]? 如何将对象转换为数组并保存对象中的 id - How to convert objects to array and save the id's from the objects 从AngularJS中的对象数组中通过id获取特定对象 - Get specific object by id from array of objects in AngularJS 通过对象ID从可观察的数组中获取JSON对象 - get JSON object from knockout observable array by objects id 如何按ID从JSON对象数组中删除数组元素 - How to delete array element from JSON array of objects by ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM