简体   繁体   English

打字稿将映射元素传递给函数 onclick

[英]Typescript pass mapped element to Function onclick

I am at the beginning stage of a Typescript application, and as a proof of concept I wish to simply alert the ID of each item on a button click.我正处于 Typescript 应用程序的开始阶段,作为概念证明,我希望在单击按钮时简单地提醒每个项目的 ID。 How can I add item.ID to the onclick Alert function below?如何将 item.ID 添加到下面的 onclick Alert 功能?

<table style="background-color:#FFFFFF;width:100px;">
  {resolvedValue.map((item, index) => (
    <tr key={index}>
      <td>
      {item.id}
      </td>
      <td>
        <button onclick="javascript:alert('Adding {item.id} ');">Add</button>
      </td>
    </tr>
  ))}
</table>

Update following answer:更新以下答案:

 <table style="background-color:#FFFFFF;width:100px;">
  {resolvedValue.map((item, index) => (
    <tr key={index}>
      <td>
      {item.id}
      </td>
      <td>
        <button onClick={() => alert('Adding')}>Add</button>

      </td>
    </tr>
  ))}
</table>

Just use React's onClick function.只需使用 React 的onClick函数。 See https://codesandbox.io/s/keen-driscoll-mx8dyhttps://codesandbox.io/s/keen-driscoll-mx8dy

<table style={{ "background-color": "#FFFFFF", "width": "100px" }}>
  {resolvedValue.map((item, index) => (
    <tr key={index}>
      <td>
      {item.id}
      </td>
      <td>
        <button onClick={() => alert('Adding' + item.id)}>Add</button>
      </td>
    </tr>
  ))}
</table>

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

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