简体   繁体   English

如何将元素的属性传递给React中的函数?

[英]How do I pass an element's property to a function in React?

I'd like to pass a property of an element of mine when clicked to the onClick function that I have setup. 当我单击已设置的onClick函数时,我想传递我的元素的属性。 I don't know how to access the property of the element though. 我不知道如何访问元素的属性。 To be specific, I'd like to pass the position property of my Google Map Marker to my onClick function so that I could store the position of the marker that was clicked into an array. 具体来说,我想将Google Map Marker的position属性传递给我的onClick函数,以便可以存储单击到数组中的标记的位置。

My Marker code looks like this: 我的标记代码如下所示:

<Marker position={{lat:"some value", lng:"some value"} 
onClick{()=>markerClick(what do I pass here?)} clickable={true}}

I'd like to pass either the position or, position's lat and lng property. 我想传递position或position的lat和lng属性。 If I try passing position,I get an error stating position not defined. 如果我尝试通过位置,则会收到错误消息,指出未定义位置。 Lat and lng just give me undefined values. 纬度和经度只是给我未定义的值。

Note: position, clickable are native properties of the Marker element. 注意:position,clickable是Marker元素的本机属性。

I think in react, all data you should store in state, instead of passing data by parameter.For your case, create a position obj in state, then update in right situation(componentDidMount, componetWillMount and so on). 我认为在反应中,您应该将所有数据都存储在状态中,而不是通过参数传递数据。对于您的情况,请在状态中创建一个位置obj,然后在合适的情况下进行更新(componentDidMount,componetWillMount等)。 And when click Marker, you can just change the state.position, then react will re-render UI. 当单击“标记”时,您只需更改state.position即可,然后react将重新渲染UI。

EDITED: you can try 编辑:您可以尝试

<Marker
  position={lat:"some value", lng:"some value"} 
  onClick={ this.handler }
  clickable={ true }
/>

and in this.handler you can 在this.handler中,您可以

this.handler = (event) => {
  event.target.getAttribute('position');
}

event.target give the natvie DOM element and just get the properties by native DOM https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute event.target提供natvie DOM元素,并仅通过本机DOM获取属性https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute

You can just just add it to the callback function that you bind to the onClick. 您可以将其添加到绑定到onClick的回调函数中。 Like so (this is assuming the position is in state, but could be in props or hard-coded): 像这样(假设位置处于状态,但可以是道具或硬编码):

  render() {
    const { position } = this.state;
    return (
      <Marker position={position}
      onClick={() => this.handleClick(position)} />
    )
  }

EDIT: I've just read the comments. 编辑:我刚刚读了评论。 If you have an array of markers, you can assign the positions to the click handlers like this (assuming you set your positions in state when you receive them from your source): 如果您有一组标记,则可以将位置分配给点击处理程序,如下所示(假设您在从源头接收位置时将其设置为状态):

  render() {

    const { positions } = this.state;
    const markers = positions.map((p, i) => 
      <Marker key={i} 
              position={p} 
              onClick={() => this.handleClick(p)} />);
    return (
      <div>
        {markers}
      </div>
    )
  }

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

相关问题 如何将 function 作为属性传递给 React 组件? - How do I pass a function as a property to a React component? 如何使用 on() 将元素的 ID 和字符串传递给我的函数? - How do I pass in an element's ID and a string to my function using on()? 如何通过 onClick function 在该上下文 onClick function 中创建在该上下文 onClick - How do I pass a React Context's state through an onClick function created in a method inside that context class? 在Angular中,我如何$ eval()一个函数并将元素传递给父函数? - In Angular how do I $eval() a function and pass the element to parent function? 如何在 React 中将整个图像元素作为 prop 传递? - How do I pass an entire image element as a prop in React? Mercury-如何设置选项元素的“ selected”属性? - Mercury - How do I set an option element's 'selected' property? 如何将对d3元素的引用传递给函数? - How do I pass a reference to a d3 element to function? 如何在设置 function 之外传递 div 元素 - How do I pass the div element outside setup function 如何将 HTML 元素作为参数传递给 Javascript 函数? - How do I pass an HTML element as an argument to a Javascript function? 如何在 React Native 中将函数作为属性传递给组件 - How to pass a function to a component as a property in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM