React 获取 JSX 中的特定元素

海克·萨法良

所以我有一个这样的 jsx

<img
  onClick={(event) => this.handleImgClick(event)}
  id="typeLightBox"
  className={`${this.state.popupType === 'typeLightBox' ? 'selected' : ''}`}
  src="url"
/>

在 className 定义中,我想知道是否有任何方法可以获得此特定 img 的 id 而不是手动编写 id?

托尔

最简单的解决方案可能是将它放在一个变量中:

const id = "typeLightBox";
return <img
  onClick={(event) => this.handleImgClick(event)}
  id={id}
  className={`${this.state.popupType === id ? 'selected' : ''}`}
  src="url"
/>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章