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();
},
render() {
return (
<div onClick={this.handleClick}></div>
);
}
});
export default Contacts;
|
登入後複製
總結
總的來說使用ES6來創建元件的語法更加簡潔,這種語法避免了過多的React樣板程式碼,而更多的使用純javascript語法,更符合javascript語法習慣。 React官方並沒有強制性要求使用哪種語法,根據需要合理的選擇即可。
【相關推薦:javascript影片教學、程式設計影片】
以上是react.js是用es6寫的嗎的詳細內容。更多資訊請關注PHP中文網其他相關文章!