How to access custom attributes from event object in React?

前端 未结 15 2024
不思量自难忘°
不思量自难忘° 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:11

    I 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() {
       ...
          
       ...},
    

    For more reference on Object destructuring https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring

提交回复
热议问题