Most efficient way to pass parameters in onClick handler

后端 未结 4 413
生来不讨喜
生来不讨喜 2021-01-02 04:09

I am using inline arrow function to change the onClick handlers of some divs in my React component, but I know it is not a good way in terms of performance.

4条回答
  •  孤独总比滥情好
    2021-01-02 04:40

    You can use arrow function to define your changeRoute handler.

    This is known as Class field syntax. More on it here in official react docs.

    constructor() {
      super(props)
    }
    
    changeRoute = (parameter) => (event) => {
        // business logic for route change.
    }
    

    Then you can use this function directly like so:

    render() {
      return (
        <>
          
    1
    2
    ) }

    You do not have to worry about the binding. Arrow functions inherit their parent's this.

提交回复
热议问题