简体   繁体   English

Es6 React Arrow函数行为

[英]Es6 React Arrow function behaviour

I'm trying to get into React using Borwserify, Watchify, Babelfiy (with ES2015 preset). 我正在尝试使用Borwserify,Watchify,Babelfiy(已预设ES2015)进入React。

Could anyone please explain, why this is working... 任何人都可以解释一下,这为什么起作用...

export default class HelloWorld extends React.Component {

constructor(props) {
    super(props);
    this.state = {
        username: 'Tyler McGinnis'
    }

    this.handleChange = (e) => {
        this.setState({username: e.target.value})
    };


}

render() {

    return (
        <div>
            Hello {this.state.username} <br />
            Change Name: <input type="text" value={this.state.username} onChange={this.handleChange} />
        </div>
    )
}

and this code isn't (arrow function outside constructor)... 而且此代码不是(构造函数外部的箭头功能)...

export default class HelloWorld extends React.Component {

constructor(props) {
    super(props);
    this.state = {
        username: 'Tyler McGinnis'
    }
}


handleChange = (e) => {
   this.setState({username: e.target.value})
};

render() {

    return (
        <div>
            Hello {this.state.username} <br />
            Change Name: <input type="text" value={this.state.username} onChange={this.handleChange} />
        </div>
    )
}

The last one gives me an error, that is: 最后一个给我一个错误,那就是:

Unexpected token (18:17)

handleChange = ((e) => {
            ^
       this.setState({username: e.target.value})
} );

All I can find on the Internet tells me, that both syntaxes should work, but they don't. 我在网上可以找到的所有信息都告诉我,这两种语法都应该起作用,但它们却不能。 this happens with the ES2015 babel preset set correctly (proof by compiling version one without an issue). 正确设置ES2015 babel预设会发生这种情况(通过编译第一个版本来证明没有问题)。

What am I missing? 我想念什么? Why can't I have an arrow function outside the constructor (or probably any other "regular" function in the class)? 为什么在构造函数之外不能有arrow函数(或者类中可能还有其他“常规”函数)?

Any help will be appreciated! 任何帮助将不胜感激! Thank you! 谢谢!

In ES6 / ES2015 you can't have arrow functions as a class method. 在ES6 / ES2015中,不能将箭头功能用作类方法。

Define handleChange as normal method. 将handleChange定义为常规方法。

handleChange(e){
   this.setState({username: e.target.value});
}

Bind handleChange with this in the constructor function 在构造函数中将此绑定到handleChange

constructor() {
    this.handleChange = this.handleChange.bind(this);
}

这是ES7 Stage-1的一个名为Class properties transform的功能 ,您可以安装babel-preset-stage-1或使用插件transform-class-properties使其工作。

if you want to use ES7 features, you need the babel plugin 如果要使用ES7功能,则需要babel插件

transform-class-properties( you need this one for arrow functions) transform-class-properties(箭头功能需要此属性)

  • yarn add babel-plugin-transform-class-properties --dev 纱线添加babel-plugin-transform-class-properties --dev
  • yarn add babel-plugin-transform-object-rest-spread --dev 纱线添加babel-plugin-transform-object-rest-spread --dev

and you config webpack something else like this 然后您配置webpack像这样的其他东西

.configureBabel(function(babelConfig) {
        babelConfig.plugins = ["transform-object-rest-spread","transform-class-properties"]
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM