简体   繁体   English

我如何访问在react状态下设置的对象数组中的属性?

[英]How can I access a property in an array of objects that is set in the state in react?

I am trying to make a simple map app that has a no of markers that triggers an info box on click , I'm using react-google-maps library , right now I am trying to handle the info box on click part ,I added an isOpen property to each marker which value should be false by default and when it is true the infoBox should appear . 我正在尝试制作一个简单的地图应用程序,该应用程序没有标记,不会在点击时触发一个信息框,我正在使用react-google-maps库,现在我正在尝试处理点击部分的信息框,我添加了每个标记的isOpen属性,默认情况下该值应为false,当该值为true时,应显示infoBox。 this is the state: 这是状态:

 state = { markers: [ { "id": "Abu-Simbel", "position":{ lat: 22.337232, lng: 31.625799 }, "isOpen":false }, { "id": "Karnak-Temples", "position":{ lat: 25.718835, lng: 32.65727 }, "isOpen":false }, { "id": "Luxor-Temple", "position":{ lat: 25.699502, lng: 32.639051 }, "isOpen":false }, { "id": "Edfu-Temple", "position":{ lat: 24.977929, lng: 32.87337 }, "isOpen":false }, { "id": " Phiale-Temple", "position":{ lat: 24.025171, lng: 32.884643 }, "isOpen":false }, { "id": " Kom-Ombo-Temple", "position":{ lat: 24.452133, lng: 32.928432 }, "isOpen":false } ] } 

and this is the handleClick function: 这是handleClick函数:

  handleMarkerTap=(marker)=>{ if (marker.isOpen === false) { marker.isOpen = true } else if (marker.isOpen === true) { marker.isOpen = false } } 

and this is the how I rendered the markers inside the map component : 这就是我在地图组件内部渲染标记的方式:

 props.markers.map( (marker)=>( <div key={marker.id}> <Marker title={marker.id} id={marker.id} position={marker.position} isOpen={marker.isOpen} onClick={ () => { props.handleMarkerTap(marker) console.log(marker.id+" is "+marker.isOpen); } } > {marker.isOpen && <InfoBox onCloseClick={props.handleMarkerTap} options={{ closeBoxURL: ``, enableEventPropagation: true }} > <div style={{ backgroundColor: `yellow`, opacity: 0.75, padding: `12px` }}> <div style={{ fontSize: `16px`, fontColor: `#08233B` }}> {marker.id} </div> </div> </InfoBox>} </Marker> </div> ) ) 

The question is how to access the isOpen property on each marker through this function . 问题是如何通过此函数访问每个标记上的isOpen属性。

When you're mutating (modifying) any state in your component (ie doing something like marker.isOpen = true ), you should be calling setState in your component herachy to trigger a re-render. 在更改(修改)组件中的任何状态时(例如,执行诸如marker.isOpen = true ),您应该在组件herachy中调用setState来触发重新渲染。 The re-render causes the UI to redraw based on the changes you made to your state. 重新渲染使UI根据您对状态所做的更改来重新绘制。

At the moment you're mutating your isOpen field, but it doesn't look like you're calling setState in relation to that. 目前,您正在对isOpen字段进行突变,但看起来并不像与此相关联地调用setState

Perhaps you could make the following changes to your component code? 也许您可以对组件代码进行以下更改? This is just one approach you could take, but it is perhaps the simplest way to achieve the re-render, without too much detail: 这只是您可以采用的一种方法,但这可能是实现重新渲染的最简单方法,而没有太多细节:

<Marker
  title={marker.id}
  id={marker.id}
  position={marker.position}
  isOpen={marker.isOpen}
  onClick={
    () => {
      props.handleMarkerTap(marker)
      console.log(marker.id+" is "+marker.isOpen);

      // Call the setState method to cause a re-render. Pass a
      // copy (achieved via [... ] syntax) of the markers array 
      // to setState to ensure that react sees the markers array 
      // as a new array rather than a reference to the same array
      // (this also encourages react to re-render)
      this.setState({ markers : [...this.state.markers] }) 
    }
  }>

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

相关问题 如何访问在状态中设置的对象数组的属性 - How Can I access a property of an object array set in state 如何在 React 中将对象数组设置为 state? - How do I set an array of objects to state in React? 如何在useState中设置对象数组state的属性? - How to set a property of an array of objects state in useState? 如何将 state 设置为反应中的对象数组 - How to set state to an array of objects in react 根据对象数组中存在的属性设置状态:React + Typescript - Set state based on an a property present in array of objects : React+Typescript 如何映射对象数组,删除一个对象并将我的状态设置为 react-native 中的新数组? - How can I map through an array of objects, remove one and set my state as the new array in react-native? 如何在 React state 的对象数组中更改一个 object 的属性? - How to change property of one object in an array of objects in React state? 如何从处于反应状态的数组内的对象中删除属性 - How to remove property from objects inside array in react state 我可以更改数组内对象属性的 state 吗? - can i change the state of property of objects inside an array? 在对象数组中,如何在 React 中按属性返回所有对象? - In an array of Objects, how do I return all Objects by property in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM