React.js中常用的ES6寫法

一 模塊

1 引入模塊以便使用

用import實(shí)現(xiàn):

import '模塊文件地址'
import 組件 from '模塊文件地址'

2 導(dǎo)出模塊

用export default實(shí)現(xiàn):

export default class MyComponent extends Component{
    ...
}

引用:

import MyComponent from './MyComponent';

二 組件

1 定義組件

通過定義一個(gè)繼承自React.Component的class來定義一個(gè)組件類:

class Photo extends React.Component {
    render() {
       ...
    }
}

2 定義組件方法

直接用名字(){},很像java定義類方法的寫法:

class Photo extends React.Component {
    componentWillMount() {

    }
    render() {
        return (
            <Image source={this.props.source} />
        );
    }
}

3 定義組件的屬性類型和默認(rèn)屬性

統(tǒng)一使用static成員來實(shí)現(xiàn):

class Video extends React.Component {
    static defaultProps = {
        autoPlay: false,
        maxLoops: 10,
    };  // 注意這里有分號(hào)
    static propTypes = {
        autoPlay: React.PropTypes.bool.isRequired,
        maxLoops: React.PropTypes.number.isRequired,
        posterFrameSrc: React.PropTypes.string.isRequired,
        videoSrc: React.PropTypes.string.isRequired,
    };  // 注意這里有分號(hào)
    render() {
        return (
            <View />
        );
    } // 注意這里既沒有分號(hào)也沒有逗號(hào)
}

注意: 對(duì)React而言,static成員在IE10及之前版本不能被繼承,而在IE11和其它瀏覽器上可以,有時(shí)會(huì)帶來一些問題。React Native則不用擔(dān)心這個(gè)問題。

4 初始化STATE

在構(gòu)造函數(shù)中初始化(這樣可以根據(jù)需要做一些計(jì)算):

class Video extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            loopsRemaining: this.props.maxLoops,
        };
    }
}

5 把方法作為回調(diào)提供并使用
ES5下可以這么做:

//ES5
var PostInfo = React.createClass({
    handleOptionsButtonClick: function(e) {
        // Here, 'this' refers to the component instance.
        this.setState({showOptionsModal: true});
    },
    render: function(){
        return (
            <TouchableHighlight onPress={this.handleOptionsButtonClick}>
                <Text>{this.props.label}</Text>
            </TouchableHighlight>
        )
    },
});

在ES5下,React.createClass會(huì)把所有的方法都bind一遍,這樣可以提交到任意的地方作為回調(diào)函數(shù),而this不會(huì)變化。但官方現(xiàn)在認(rèn)為這是不標(biāo)準(zhǔn)、不易理解的。

ES6下,需要通過bind來綁定this引用,或者使用箭頭函數(shù)(它會(huì)綁定當(dāng)前scope的this引用)來調(diào)用:

//ES6
class PostInfo extends React.Component
{
    handleOptionsButtonClick(e){
        this.setState({showOptionsModal: true});
    }
    render(){
        return (
            <TouchableHighlight 
                onPress={this.handleOptionsButtonClick.bind(this)}
                onPress={e=>this.handleOptionsButtonClick(e)}
                >
                <Text>{this.props.label}</Text>
            </TouchableHighlight>
        )
    },
}

箭頭函數(shù)是在這里定義了一個(gè)臨時(shí)的函數(shù),箭頭函數(shù)的箭頭=>之前是一個(gè)空括號(hào)、單個(gè)的參數(shù)名、或用括號(hào)括起的多個(gè)參數(shù)名,而箭頭之后可以是一個(gè)表達(dá)式(作為函數(shù)的返回值),或者是用花括號(hào)括起的函數(shù)體(需要自行通過return來返回值,否則返回的是undefined)。

即:箭頭函數(shù)箭頭前是參數(shù),箭頭后是函數(shù)體或返回值。

注意:
不論是bind還是箭頭函數(shù),每次被執(zhí)行都返回的是一個(gè)新的函數(shù)引用,因此如果你還需要函數(shù)的引用去做一些別的事情(譬如卸載監(jiān)聽器),那么必須自己保存這個(gè)引用:

// 錯(cuò)誤的做法
class PauseMenu extends React.Component{
    componentWillMount(){
        AppStateIOS.addEventListener('change', this.onAppPaused.bind(this));
    }
    componentDidUnmount(){
        AppStateIOS.removeEventListener('change', this.onAppPaused.bind(this));
    }
    onAppPaused(event){
    }
}
// 正確的做法
class PauseMenu extends React.Component{
    constructor(props){
        super(props);
        this._onAppPaused = this.onAppPaused.bind(this);//注意這里
    }
    componentWillMount(){
        AppStateIOS.addEventListener('change', this._onAppPaused); //還有這里
    }
    componentDidUnmount(){
        AppStateIOS.removeEventListener('change', this._onAppPaused);
    }
    onAppPaused(event){
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容