React click handlers and binding this

前端 未结 2 1275
清酒与你
清酒与你 2021-01-06 22:27

I have a react component where I am iterating over a list and creating rows. In each row, there is a delete button. When the delete button is clicked, I want to pass a refer

相关标签:
2条回答
  • 2021-01-06 22:46

    You can do less verbose:

     R.td({}, R.button({onClick: this.onTagDelete.bind(this, tag.name), // BIND
                                    className: "delete"}, "\u2716")),
    
    0 讨论(0)
  • 2021-01-06 22:51

    I'm fairly new to react, but I figured I'd throw this out here to help.

    I think you need to change this line,

    R.td({}, R.button({onClick: function() {this.onTagDelete(tag.name)}.bind(this), // BIND
                                className: "delete"}, "\u2716")),
    

    to

    R.td({}, R.button({onClick: function() {this.onTagDelete.bind(this, tag.name)}, // BIND
                                className: "delete"}, "\u2716")),
    

    I'm pretty sure that this should now pass the tag name to the function. Another way of getting data from the clicked subject is through refs, but with lists of items I don't think this works well because of repeated ref names. So I would just do what you are doing now.

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