简体   繁体   English

React 基础 - 将 Props 传递给 Function

[英]React Basics - Passing Props to Function

I am very new to React so please forgive me if this is very basic stuff.我对 React 很陌生,所以如果这是非常基本的东西,请原谅我。

I have a component that renders the following (there could be 100s of this on a page)我有一个呈现以下内容的组件(页面上可能有 100 个)

<a onClick={this.handlePropChange} propRef={this.props.proposal.PROPREF} >View</a>  

When i click the button i want to load a modal and pass through the value of propRef to a modal component (so i can then use some API calls to access information relating to that proposal).当我单击按钮时,我想加载一个模态并将 propRef 的值传递给模态组件(这样我就可以使用一些 API 调用来访问与该提案相关的信息)。

handlePropChange(event) {
    alert(event.target.propRef);
    $('#proposalModal').modal('show');
}

Can someone tell me what i am doing wrong?有人可以告诉我我做错了什么吗? Like i said i know this is basic but i'm struggling today.就像我说的,我知道这是基本的,但我今天很挣扎。

2 solutions: 2个解决方案:

  • Using a data attribute使用数据属性
<a onClick={this.handlePropChange} data-prop-ref={this.props.proposal.PROPREF}>
  View
</a>

handlePropChange(event) {
  alert(event.target.dataset.propRef);
  ...
}
  • Passing propRef as a parameter of handlePropChange function传递 propRef 作为handlePropChange function 的参数
<a onClick={() => this.handlePropChange(this.props.proposal.PROPREF)}>
  View
</a>

handlePropChange(propRef) {
  alert(propRef);
  ...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM