Skip to content

Instantly share code, notes, and snippets.

@brenopolanski
Created March 25, 2019 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brenopolanski/f8606302ee8cab8ee9cac5d8adf4b075 to your computer and use it in GitHub Desktop.
Save brenopolanski/f8606302ee8cab8ee9cac5d8adf4b075 to your computer and use it in GitHub Desktop.
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