'React - ES6 way' of binding click handlers with parameters

后端 未结 5 2096
暗喜
暗喜 2021-02-09 12:52

I\'ve read a bunch of articles about the use of () => {} syntax, binding in the constructor, binding in the props etc.. but from what I understand, binding

5条回答
  •  攒了一身酷
    2021-02-09 13:20

    For your

  • {item.name}
  • this typically means you need another layer of abstraction IE a new react component that returns the same element but you can pass in the onClick as a prop as well as item id as a prop. Then in that component you would call this.props.onClick(this.props.id) or however you format the data.

    this article draws out all the differences between each way of binding instance methods as well as how each impacts performance https://medium.com/@housecor/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56#.

提交回复
热议问题