亿信开发者
我们一直在努力

react.js是用es6写的吗

ptgidc

react.js是用es6写的,可以用Babel转译为ES5,也可以用Babel转译为JavaScript的JSX;由于React的设计思想极其独特,属于革命性创新,性能出众,代码逻辑却非常简单。使用ES6来创建组件的语法更加简洁,这种语法避免了过多的React样板代码,而更多的使用纯javascript语法,更符合javascript语法习惯。

 

本教程操作环境:windows7系统、ECMAScript 6&&react17版、Dell G3电脑。

ReactJS是构建视图最流行的前端库,ReactJS是用ES6写的,可以用Babel转译为ES5,也可以用Babel转译为JavaScript的JSX。由于React的设计思想极其独特,属于革命性创新,性能出众,代码逻辑却非常简单。所以,越来越多的人开始关注和使用,它可能是将来Web开发的主流工具。

React使用ES6和ES5写法对比

创建组件

ES6 class创建的组件语法更加简明,也更符合javascript。内部的方法不需要使用function关键字。

React.createClass

1

2

3

4

5

6

7

8

9

10

11

import React from 'react';

const MyComponent = React.createClass({

  render: function() {

    return (

      <div>以前的方式创建的组件</div>

    );

  }

});

export default MyComponent;

React.Component(ES6)

1

2

3

4

5

6

7

8

9

10

11

import React,{ Component } from 'react';

class MyComponent extends Component {

  render() {

    return (

      <div>ES6方式创建的组件</div>

    );

  }

}

export default MyComponent;

props propTypes and getDefaultProps

  • 使用React.Component创建组件,需要通过在constructor中调用super()将props传递给React.Component。另外react 0.13之后props必须是不可变的。
  • 由于是用ES6 class语法创建组件,其内部只允许定义方法,而不能定义属性,class的属性只能定义在class之外。所以propTypes要写在组件外部。
  • 对于之前的getDefaultProps方法,由于props不可变,所以现在被定义为一个属性,和propTypes一样,要定义在class外部。

React.createClass

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import React from 'react';

const MyComponent = React.createClass({

  propTypes: {

    nameProp: React.PropTypes.string

  },

  getDefaultProps() {

    return {

      nameProp: ''

    };

  },

  render: function() {

    return (

      <div>以前的方式创建的组件</div>

    );

  }

});

export default MyComponent;

React.Component(ES6)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

import React,{ Component } from 'react';

class MyComponent extends Component {

  constructor(props) {

    super(props);

  }

  render() {

    return (

      <div>ES6方式创建的组件</div>

    );

  }

}

MyComponent.propTypes = {

  nameProp: React.PropTypes.string

};

MyComponent.defaultProps = {

  nameProp: ''

};

export default MyComponent;

State

使用ES6 class语法创建组件,初始化state的工作要在constructor中完成。不需要再调用getInitialState方法。这种语法更加的符合JavaScript语言习惯。

React.createClass

1

2

3

4

5

6

7

8

9

10

import React from 'react';const MyComponent = React.createClass({

  getInitialState: function() {

    return { data: [] };

  },

  render: function() {

    return (

      <div>以前的方式创建的组件</div>

    );

  }});export default MyComponent;

React.Component(ES6)

1

2

3

4

5

6

7

8

9

10

11

import React,{ Component } from 'react';class MyComponent extends Component {

  constructor(props) {

    super(props);

    this.state = { data: [] };

  }

  render() {

    return (

      <div>ES6方式创建的组件</div>

    );

  }}export default MyComponent;

this

使用ES6 class语法创建组件, class中的方法不会自动将this绑定到实例中。必须使用 .bind(this)或者 箭头函数 =>来进行手动绑定。

React.createClass

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import React from 'react';

const MyComponent = React.createClass({

  handleClick: function() {

    console.log(this);

  },

  render: function() {

    return (

      <div onClick={this.handleClick}>以前的方式创建的组件</div>

    );

  }

});

export default MyComponent;

React.Component(ES6)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import React,{ Component } from 'react';

class MyComponent extends Component {

  handleClick() {

    console.log(this);

  }

  render() {

    return (

      <div onClick={this.handleClick.bind(this)}>ES6方式创建的组件</div>

    );

  }

}

export default MyComponent;

也可以将绑定方法写到constructor中:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

import React,{ Component } from 'react';

class MyComponent extends Component {

  constructor(props) {

    super(props);

    this.handleClick = this.handleClick.bind(this);

  }

  handleClick() {

    console.log(this);

  }

  render() {

    return (

      <div onClick={this.handleClick}>ES6方式创建的组件</div>

    );

  }

}

export default MyComponent;

或者使用箭头函数 => :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

mport React,{ Component } from 'react';

class MyComponent extends Component {

  handleClick = () => {

    console.log(this);

  }

  render() {

    return (

      <div onClick={this.handleClick}>ES6方式创建的组件</div>

    );

  }

}

export default MyComponent;

Mixins

使用ES6语法来创建组件是不支持React mixins的,如果一定要使用React mixins就只能使用React.createClass方法来创建组件了。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import React,{ Component } from 'react';

var SetIntervalMixin = {

  doSomething: function() {

    console.log('do somethis...');

  },

};

const Contacts = React.createClass({

  mixins: [SetIntervalMixin],

  handleClick() {

    this.doSomething(); //使用mixin

  },

  render() {

    return (

      <div onClick={this.handleClick}></div>

    );

  }

});

export default Contacts;

总结

总的来说使用ES6来创建组件的语法更加简洁,这种语法避免了过多的React样板代码,而更多的使用纯javascript语法,更符合javascript语法习惯。React官方并没有强制性要求使用哪种语法,根据需要合理的选择即可。

赞(1)
未经允许不得转载:亿信开发者 » react.js是用es6写的吗