React Native component callback functions

后端 未结 2 563
情歌与酒
情歌与酒 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:00

     {doSomething(data)} }>
    

    Is calling a new anonymous function onPress, which will run doSomething.

    
    

    Is calling a reference to a named function, that you have already defined in your class. The binding to this is required only when you are working with class function (I mean, non ES6 arrow function).

    const doSomething = () => { ... }
    

    Would not require a .bind(this), since arrow functions bind on lexical scope.

    You should definately read over What is the best and most efficient way to bind callbacks in ReactJS? In the constructor or in the render method or as a property initializer?

提交回复
热议问题