Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using event.target with React components

event.target gives you the native DOMNode, then you need to use the regular DOM APIs to access attributes. For instance getAttribute or dataset.

<button 
  data-space="home" 
  className="home" 
  data-txt="Home" 
  onClick={ this.props.onClick } 
/> 
  Button
</button>

onClick(e) {
   console.log(e.target.dataset.txt, e.target.dataset.space);
   console.log(e.target.getAttribute('data-txt'), e.target.getAttribute('data-space');
}

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.