V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
123s
V2EX  ›  Node.js

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

  •  
  •   123s · 2016-03-09 09:28:05 +08:00 · 4301 次点击
    这是一个创建于 2976 天前的主题,其中的信息可能已经有所发展或是发生改变。
    难道要加--harmony_arrow_functions 才能吗?但是我是用命令来启动RN,怎么加上呢?
    6 条回复    2016-03-09 16:11:26 +08:00
    WildCat
        1
    WildCat  
       2016-03-09 09:31:01 +08:00
    不需要加吧, react native 用 babel
    123s
        2
    123s  
    OP
       2016-03-09 09:33:26 +08:00
    @WildCat

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

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

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

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

    除非你用 with statement 否则不存在可以省略 this 的情况
    neoblackcap
        6
    neoblackcap  
       2016-03-09 16:11:26 +08:00
    @yyfearth 应该说箭头函数的 this 跟包围它的的代码块里面的 this 是共享的吧。毕竟说没有 this 其他初学者可能会以为 this 变量就不能用了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   975 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 23:38 · PVG 07:38 · LAX 16:38 · JFK 19:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.