React能够呈现自定义属性 http://facebook.github.io/react/docs/jsx-gotchas.html:

如果要使用自定义属性,则应该在其前面加上 数据- - - - - -。 <div data-custom-attribute="foo" />

这是一个好消息,除了我找不到一种方法来访问它从事件对象,例如:

render: function() {
...
<a data-tag={i} style={showStyle} onClick={this.removeTag}></a>
...
removeTag: function(event) {
    this.setState({inputVal: event.target????}); 
},

元素和数据属性在html中呈现良好。像style这样的标准属性可以通过event.target.style fine访问。 而不是事件。我试了试:

 event.target.props.data.tag
 event.target.props.data["tag"]
 event.target.props["data-tag"]  
 event.target.data.tag
 event.target.data["tag"]
 event.target["data-tag"]

这些都没用。


当前回答

<div className='btn' onClick={(e) =>
     console.log(e.currentTarget.attributes['tag'].value)}
     tag='bold'>
    <i className='fa fa-bold' />
</div>

所以e.currentTarget.attributes['标签']。价值对我有用

其他回答

在React中,你不需要html数据,使用函数返回另一个函数;像这样,它是非常简单的发送自定义参数,你可以访问自定义数据和事件。

render: function() {
...
<a style={showStyle} onClick={this.removeTag(i)}></a>
...
removeTag: (i) => (event) => {
    this.setState({inputVal: i}); 
},

如果你有多个不同数据标签(年龄,姓名,电子邮件)的图标:

       <button
          data-label="name" 
          onMouseOver={handleValue}
          className="icon"
        >
          <FaUser />
        </button>

当鼠标停留在图标上时,可以通过访问data-label来更改标题

const handleValue = (e) => {
    // making sure mouse is over an icon
    if (e.target.classList.contains("icon")) {
      const newValue = e.target.dataset.label;
      setTitle(newValue);
      setValue(person[newValue]);
    }
  };

或者你可以使用闭包:

render: function() {
...
<a data-tag={i} style={showStyle} onClick={this.removeTag(i)}></a>
...
},
removeTag: function (i) {
    return function (e) {
    // and you get both `i` and the event `e`
    }.bind(this) //important to bind function 
}

您可以像这样访问数据属性

event.target.dataset.tag

这对我很管用……在本例中,我的属性名为“attr”。

e.target.selectedOptions[0].attributes.attr.value