How to access custom attributes from event object in React?

前端 未结 15 1927
不思量自难忘°
不思量自难忘° 2020-11-27 09:27

React is able to render custom attributes as described at http://facebook.github.io/react/docs/jsx-gotchas.html:

If you want to use a custom attribut

相关标签:
15条回答
  • 2020-11-27 10:13

    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').

    https://facebook.github.io/react/docs/events.html

    It looks like React does this so that it works across more browsers. You can access the old properties through a nativeEvent attribute.

    0 讨论(0)
  • 2020-11-27 10:15

    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 
    
    0 讨论(0)
  • 2020-11-27 10:17

    You can simply use event.target.dataset object . This will give you the object with all data attributes.

    0 讨论(0)
提交回复
热议问题