react-native 语法的问题

onMomentumScrollEnd={
(e)=>this.onAnimationEnd(e)
}

onScrollBeginDrag={
this.onScrollBeginDrag
}语法为什么不相同 是传参的原因吗
(e)=>this.onAnimationEnd(e)这个是什么意思 属于什么语法 是ES6=>这个吗

阅读 3k
1 个回答

你需要提出这是在react中jsx写法,要不然你的两段代码是不能用的。。。估计有两个downvote也是因为这个原因。。。

应该这么写才能表清楚。
<Component onMomentumScrollEnd={(e)=>this.onAnimationEnd(e)} onScrollBeginDrag={this.onScrollBeginDrag} />

现在根据上面代码讲一下:
onMomentumScrollEnd={(e)=>this.onAnimationEnd(e)} 代表了把(e)=>this.onAnimationEnd(e)当作onMomentumScrollEnd的值传给下级组件

(e)=>this.onAnimationEnd(e)是ES6的箭头函数写法,而且箭头函数会自动绑定this,等同于

(function(e){
  return this.onAnimationEnd(e);
}).bind(this)

onScrollBeginDrag={this.onScrollBeginDrag} 就是把this.onScrollBeginDrag作为onScrollBeginDrag的值传给子组建

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题