React Native component callback functions

后端 未结 2 587
情歌与酒
情歌与酒 2021-01-14 10:54

In components, I\'ve seen different way of doing callbacks. What is the difference between:

 {doSomething(data)} }>
         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-14 11:04

    {doSomething(data)} }>

    This code block uses the ES6 Arrow function; which is another way of declaring a function in javascript. Also, the scope of this in arrow function depend where the function was created as opposed to normal scoping rule of this which by default depends on how the function was called.

    This statement makes a call to doSomething method. But since the event registration is done on different element, the Scope of doSomething is different and is forcefully binded by using bind method in javascript.

    Also, in the second method you are not passing the data parameter, which you can pass using the second argument to the method like shown below.

提交回复
热议问题