ReactNative開發——組件的生命週期

來源:互聯網
上載者:User
ReactNative開發——組件的生命週期 使用ES6文法建立Component

我查看了下網上有關React Native中組件的生命週期有關的資料,發現大多介紹的是 ReactClass.create這種方式建立的Component建立的組件,這種方式建立的組件,生命週期都是先執行和getDefaultProps和 getInitialState。

而我建立Component的方式採用了ES6的寫法,自訂class繼承自 React.Component的方式。這種方式不支援getDefaultProps和getInitialState這種方式來擷取 props和state。而是使用類中的屬性來擷取。

使用es6的寫法:用 static defaultProps 屬性代替原先的getDefaultProps擷取屬性和這種方式;
用state屬性代替原先的getInitialState這種方式;

樣本:

class Note extends Component {    constructor() {        super();    }    // getInitialState(){    //     console.log('getInitialState.');    //  return this.state的初始值;    // }    //    // getDefaultProps(){    //     console.log('getDefaultProps.');    //  return this.props的初始值    // }    //    // 以上2個函數,只能在 React.createClass()方式下使用。    // 當前使用 extends Component這種方式下,我們用屬性 state代替 getInitialState函數,    // 用 static defaultProps 屬性代替 getDefaultProps。    //    /**     * 設定this.props的初始值。     *     * @type {{value: string}}     */    static defaultProps = {value: 'this is default props'}    /**     * 設定this.state的初始值。     *     * @type {{value: string}}     */    state = {value: 'this is initial state.'}
生命週期圖

我畫了一張組件的聲明循環圖表,圖比較醜~大家別介意哈~,額~ 如果大神有什麼好的流程圖工具可以留言給我介紹。

聲明周期我們將它分成了三部分:
1. 初始化與掛載
執行建構函式->componentWillMount->render->componentDidMount
這個過程在組件的生命週期中只執行一次。等componentDidMount執行完畢之後,組件就進入了運行狀態。
2. 運行階段
當props發生改變後,會先回調componentWillReceiveProps並傳入新的props。然後會執行更新介面相關的一系列回調方法。
當state發生改變後,會執行更新相關的回調方法。
更新相關的回掉方法:
1、shouldComponentUpdate 該函數會返回一個bool值用來決定是否需要更新介面。
2、componentWillUpdate 更新前會回調這個函數。
3、render再次執行這個函數,這個函數返回子組件資訊。
4、componentDidUpdate 等待更新完成之後回調
3. 卸載階段
當組件將要被卸載時,回先回調componentWillUnmount函數。 生命週期相關的函數

函數 說明
void componentWillMount() 該函數只會被執行一次,它在初始化渲染之前執行,當它執行完成之後,render函數會馬上被React Native架構調用
ReactClass render() 該函數會被執行多次,是一個很重要的函數,用來返回我們的UI組件資訊
void componentDidMount() 該函數之後執行一次,他在組件初始化渲染完成之後被調用
void componentWillReceiveProps(nextProps) 如果ReactNative組件收到新的props時,這個函數會被回調,回調的參數nextProps就是新的props
bool shouldComponentUpdate(nextProps, nextState) 如果ReactNative組件收到新的props或者新的state時,這個函數將被調用,它接收2個參數,第一個是新的props,第二個是新的state,和這個函數還需要返回一個bool值,用來告訴ReactNative架構針對這次改變,ReactNative是否需要重新渲染本組件
void componentWillUpdate(nextProps, nextState) 在ReactNative架構重新渲染ReactNative組件之前會調用這個函數。開發人員可以在和這個函數中為即將發生的重新渲染做一些準備,但開發人員不能在這個函數中通過this.setState再次改變狀態機器變數的值
void componentDidUpdate(prevProps, prevState) React Native架構在重新渲染ReactNative組件完成之後調用這個函數,傳入兩個參數時渲染前的props和state
void componentWillUnmount() 在React Native組件被卸載之前這個函數將執行
代碼

下面是我測試生命組件生命週期的代碼:

/** * Created by blueberry on 6/5/2017. * * 參考源碼:ReactClass.js */import React, {Component} from 'react'import ReactNative, {AppRegistry, View, Text, Button} from 'react-native'class Note extends Component {    constructor() {        super();    }    // getInitialState(){    //     console.log('getInitialState.');    //  return this.state的初始值;    // }    //    // getDefaultProps(){    //     console.log('getDefaultProps.');    //  return this.props的初始值    // }    //    // 以上2個函數,只能在 React.createClass()方式下使用。    // 當前使用 extends Component這種方式下,我們用屬性 state代替 getInitialState函數,    // 用 static defaultProps 屬性代替 getDefaultProps。    //    /**     * 設定this.props的初始值。     *     * @type {{value: string}}     */    static defaultProps = {value: 'this is default props'}    /**     * 設定this.state的初始值。     *     * @type {{value: string}}     */    state = {value: 'this is initial state.'}    /**     * 最先執行的回呼函數,該函數在組件的聲明周期中只執行一次,這個函數執行完之後,ReactNative馬上會調用組件的render方法。     */    componentWillMount() {        console.log('componentWillMount');    }    /**     * 用來渲染介面。     *     * @returns {XML}     */    render() {        console.log('render');        return (            <View >                <Text>                    屬性初始值:{this.props.value} {'\n'} 狀態初始值:{this.state.value}                </Text>                <Button title="改變state" onPress={() => {                    this.setState({value: 'i changed'})                }}/>            </View>        )    }    /**     * 該函數在組件的聲明周期中只執行一次,它在初始化渲染完成之後執行。     */    componentDidMount() {        console.log('componentDidMount');    }    /**     * 在組件渲染完成之後,當reactNative組件接受到新的props時,這個函數將被調用,這個函數接受一個object參數,object裡時新的props。     * @param nextProps 新的props     */    componentWillReceiveProps(nextProps) {        console.log('componentWillReceiveProps' + nextProps.value);    }    /**     * 在組件渲染完成之後,當ReactNative組件接收到新的state或props時,這個函數將被調用。它接收兩個object參數,其中第一個是新的     * props;第二個是新的state。函數還需要返回一個bool值,告訴ReactNative針對這次改變,ReactNative是否需要重新渲染本組件。如果     * 函數返回false,React Native 將不會重新渲染本組件。     * @param nextProps 新的props     * @param nextState 新的state     * @returns {boolean} 是否需要重新渲染本組件     */    shouldComponentUpdate(nextProps, nextState) {        console.log('shouldComponentUpdate');        return true;    }    /**     * ReactNative組件初始化渲染完成後,React Native架構在ReactNative組件重新渲染組件前,會調用這個函數。     * @param nextProps 新的props     * @param nextState 新的state     */    componentWillUpdate(nextProps, nextState) {        console.log('componentWillUpdate');    }    /**     * ReactNative組件初始化渲染完成之後,ReactNative架構在重新渲染組件完成之後會調用這個函數。     * @param prevProps 新的props     * @param prevState 新的state     */    componentDidUpdate(prevProps, prevState) {        console.log('componentDidUpdate');    }    /**     * 在該組件被卸載之前調用。     * 這個函數沒有參數,也不需要傳回值。     */    componentWillUnmount() {        console.log('componentWillUnmount');    }}/** * 為了測試改變Note 組件的props後的回呼函數是否執行,所有定義了一個NoteWrapper類。 */export default class NoteWrapper extends Component {    state = {value: 'from parent'}    render() {        return (            <View style={{backgroundColor: 'grey', flex: 1,}}>                <Note                    {...this.state}                />                <Button title="改變props" onPress={() => {                    this.setState({value: 'changed'});                }}/>            </View>        )    }}AppRegistry.registerComponent('Note', () => Note);// 如果需要測試改變組件的props,componentWillReceiveProps是否執行,可使用下面這個一行代碼測試// AppRegistry.registerComponent('Note', () => NoteWrapper);
參考

https://facebook.github.io/react/docs/react-component.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.