when to use function() , function or () => function(callback)

后端 未结 3 1767
渐次进展
渐次进展 2021-01-21 08:12

I have been searching for a while for a good explanation so its all clear to me. Example:

this.onDeleteHandler(index)}/>
3条回答
  •  攒了一身酷
    2021-01-21 08:19

    this.onDeleteHandler(index)}/>
    

    It passes anonymous function as a callback which - when clicked - triggers onDeleteHandler with extra index parameter (which has to be defined in the scope before).

    
    

    It passes result of onDeleteHandler() as a callback - probably a bad idea - onDeleteHandler function has to return another function that will be invoked on click.

     this.nameChangedhandler(event, person.id)} />
    

    Looks invalid - will result with syntax error.

    
    

    Similar to the first example but doesn't take custom parameters. Default click event will be passed as a first argument for onDeleteHandler

提交回复
热议问题