123s
V2EX  ›  Node.js

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

  •  
  •   123s · Mar 9, 2016 · 4883 views
    This topic created in 3795 days ago, the information mentioned may be changed or developed.
    难道要加--harmony_arrow_functions 才能吗?但是我是用命令来启动RN,怎么加上呢?
    6 replies    2016-03-09 16:11:26 +08:00
    WildCat
        1
    WildCat  
       Mar 9, 2016
    不需要加吧, react native 用 babel
    123s
        2
    123s  
    OP
       Mar 9, 2016
    @WildCat

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

    我这样写会报 setState 不是函数。 arrow function 应该不用 bind this 吧。
    WildCat
        3
    WildCat  
       Mar 9, 2016
    this.setState()

    绝对不会是直接 setState
    123s
        4
    123s  
    OP
       Mar 9, 2016
    @WildCat

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

    除非你用 with statement 否则不存在可以省略 this 的情况
    neoblackcap
        6
    neoblackcap  
       Mar 9, 2016
    @yyfearth 应该说箭头函数的 this 跟包围它的的代码块里面的 this 是共享的吧。毕竟说没有 this 其他初学者可能会以为 this 变量就不能用了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   968 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 19:28 · PVG 03:28 · LAX 12:28 · JFK 15:28
    ♥ Do have faith in what you're doing.