React Native 中使用箭头函数,好像不认识。

2016-03-09 09:28:05 +08:00
 123s
难道要加--harmony_arrow_functions 才能吗?但是我是用命令来启动RN,怎么加上呢?
4341 次点击
所在节点    Node.js
6 条回复
WildCat
2016-03-09 09:31:01 +08:00
不需要加吧, react native 用 babel
123s
2016-03-09 09:33:26 +08:00
@WildCat

onPress={ ()=> setState({name:'xx'})} ..

我这样写会报 setState 不是函数。 arrow function 应该不用 bind this 吧。
WildCat
2016-03-09 09:38:26 +08:00
this.setState()

绝对不会是直接 setState
123s
2016-03-09 09:50:14 +08:00
@WildCat

昨天也都试过,原来是我看错了。一直去改变另外一个 state ,难怪没用。
yyfearth
2016-03-09 11:10:27 +08:00
@123s 肯定要加 this 啊
arrow function 应该不用 bind this 的意思是 这函数本身没有 this
它的 this 是外面 function 的 this
但不等于说可以把 this 省略

除非你用 with statement 否则不存在可以省略 this 的情况
neoblackcap
2016-03-09 16:11:26 +08:00
@yyfearth 应该说箭头函数的 this 跟包围它的的代码块里面的 this 是共享的吧。毕竟说没有 this 其他初学者可能会以为 this 变量就不能用了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/262082

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX