React is able to render custom attributes as described at :
If you want to use a custom attribute, you should prefix it with data-.
<div />
And that's great news except I can't find a way to access it from the event object e.g.:
render: function() { ... <a data-tag={i} style={showStyle} onClick={this.removeTag}></a> ... removeTag: function(event) { this.setState({inputVal: event.target????}); }, The element and data- property render in html fine. Standard properties like style can be accessed as event.target.style fine. Instead of event.target I tried:
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"] none of these worked.
115 Answers
event.target gives you the native DOM node, then you need to use the regular DOM APIs to access attributes. Here are docs on how to do that:Using data attributes.
You can do either event.target.dataset.tag or event.target.getAttribute('data-tag'); either one works.
To help you get the desired outcome in perhaps a different way than you asked:
render: function() { ... <a data-tag={i} style={showStyle} onClick={this.removeTag.bind(null, i)}></a> ... }, removeTag: function(i) { // do whatever }, Notice the bind(). Because this is all javascript, you can do handy things like that. We no longer need to attach data to DOM nodes in order to keep track of them.
IMO this is much cleaner than relying on DOM events.
Update April 2017: These days I would write onClick={() => this.removeTag(i)} instead of .bind
Here's the best way I found:
var attribute = event.target.attributes.getNamedItem('data-tag').value; Those attributes are stored in a "NamedNodeMap", which you can access easily with the getNamedItem method.
2Or you can use a closure :
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 } 2// Method inside the component userClick(event){ let tag = event.currentTarget.dataset.tag; console.log(tag); // should return Tagvalue } // when render element <a onClick={this.userClick}>Click me</a> 1<div className='btn' onClick={(e) => console.log(e.currentTarget.attributes['tag'].value)} tag='bold'> <i className='fa fa-bold' /> </div> so e.currentTarget.attributes['tag'].value works for me
As of React v16.1.1 (2017), here is the official solution:
TLDR: OP should do:
render: function() { ... <a style={showStyle} onClick={(e) => this.removeTag(i, e)}></a> ... removeTag: function(i, event) { this.setState({inputVal: i}); } 4This single line of code solved the problem for me:
event.currentTarget.getAttribute('data-tag') You can access data attributes something like this
event.target.dataset.tag If anyone is trying to use event.target in React and finding a null value, it is because a SyntheticEvent has replaced the event.target. The SyntheticEvent now holds 'currentTarget', such as in event.currentTarget.getAttribute('data-username').
It looks like React does this so that it works across more browsers. You can access the old properties through a nativeEvent attribute.
You can simply use event.target.dataset object . This will give you the object with all data attributes.
I do not know about React, but in the general case you can pass custom attributes like this:
1) define inside an html-tag a new attribute with data- prefix
data-mydatafield = "asdasdasdaad" 2) get from javascript with
e.target.attributes.getNamedItem("data-mydatafield").value 1Try instead of assigning dom properties (which is slow) just pass your value as a parameter to function that actually create your handler:
render: function() { ... <a style={showStyle} onClick={this.removeTag(i)}></a> ... removeTag = (customAttribute) => (event) => { this.setState({inputVal: customAttribute}); } 1In React you don't need the html data, use a function return a other function; like this it's very simple send custom params and you can acces the custom data and the event.
render: function() { ... <a style={showStyle} onClick={this.removeTag(i)}></a> ... removeTag: (i) => (event) => { this.setState({inputVal: i}); }, 0I think it's recommended to bind all methods where you need to use this.setState method which is defined in the React.Component class, inside the constructor, in your case you constructor should be like
constructor() { super() //This binding removeTag is necessary to make `this` work in the callback this.removeTag = this.removeTag.bind(this) } removeTag(event){ console.log(event.target) //use Object destructuring to fetch all element values'' const {style, dataset} = event.target console.log(style) console.log(dataset.tag) } render() { ... <a data-tag={i} style={showStyle} onClick={this.removeTag.bind(null, i)}></a> ...},