ReactNative从零开始笔记2-组件的生命周期

2019-01-18  本文已影响24人  摸着石头过河_崖边树

一、使用环境

二、ReactNative提供了哪些组件

更多组件使用查看ReactNative组件与API

三、ReactNative组件的生命周期
1.创建阶段,组件实例化阶段

调用顺序如下:

1.1 在实例化组件的时候调用构造函数constructor

//1.初始化调用 一次
constructor(props) {
    super(props);
    this.state = {
        age: 0,
        name: '生命周期',
    }
    console.warn('调用constructor');
}

1.2 即将加载组件的时候调用 componentWillMount

  //2.即将加载组件调用 一次
componentWillMount(): void {
    console.warn('调用componentWillMount');
}

1.3 渲染组件render

//3.渲染组件调用  多次
render() {
     console.warn('调用render()');
    return (
        <View>
            <Button title={'刷新页面'} onPress={()=>{
                this.changeAgeEvent();
            }}></Button>
            <Text>年龄:{this.state.age}</Text>
        </View>
    )
}

1.4 加载组件完成的时候调用componentDidMount

 // 4.组件加载完成调用 一次
componentDidMount(): void {
    console.warn('调用componentDidMount');
}

创建阶段效果:

image.png
2. 运行阶段

2.1 componentWillReceiveProps

 //5.改变属性时候调用props 
componentWillReceiveProps(nextProps: Readonly<P>, nextContext: any): void {
    console.warn('调用componentWillReceiveProps');
}

2.2 shouldComponentUpdate

  // 6.需要刷新组件的时候调用,比如改变props/state 控制是否刷新组件
shouldComponentUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): boolean {
    console.warn('shouldComponentUpdate');
    return true;
 }

2.3 componentWillUpdate

  // 7.组件即将刷新
componentWillUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void {
    console.warn('调用componentWillUpdate');
}

2.4 componentDidUpdate

  // 8. 组件刷新之后
componentDidUpdate(prevProps: Readonly<P>, prevState: Readonly<S>, snapshot: SS): void {
    console.warn('调用componentDidUpdate');
}

运行阶段调用效果


image.png
3. 销毁/卸载组件阶段

componentWillUnmount

  //9. 组件即将销毁的时候调用, 清楚数据
componentWillUnmount(): void {
    console.warn('调用componentWillUnmount');
}
4.其他方法

componentDidCatch

//10. render出错调用该函数 
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
    console.warn('调用componentDidCatch')
}
5.生命周期完整流程
image.png

完整生命周期代码

  class LifeCycleComponent extends Component {
 //1.初始化调用 一次
constructor(props) {
    super(props);
    this.state = {
        age: 0,
        name: '生命周期',
    }
    console.warn('调用constructor');
}
//2.即将加载组件调用 一次
componentWillMount(): void {
    console.warn('调用componentWillMount');
}

//3.渲染组件调用  多次
render() {
     console.warn('调用render()');
    return (
        <View>
            <Button title={'刷新页面'} onPress={()=>{
                this.changeAgeEvent();
            }}></Button>
            <Text>年龄:{this.state.age}</Text>
        </View>
    )
}

// 4.组件加载完成调用 一次
componentDidMount(): void {
    console.warn('调用componentDidMount');
}

// 方法 改变state age
changeAgeEvent(){
    //按钮点击一次  改变状态中age的值,进行一次render()
    this.setState((prevState, callback)=>{
        return{age: prevState.age +1}
    });
}

//5.改变属性时候调用props 没有顺序
componentWillReceiveProps(nextProps: Readonly<P>, nextContext: any): void {
    console.warn('调用componentWillReceiveProps');
}

// 6.需要刷新组件的时候调用,比如改变props/state 控制是否刷新组件
shouldComponentUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): boolean {
    console.warn('调用shouldComponentUpdate');
    return true;
}

// 7.组件即将刷新
componentWillUpdate(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void {
    console.warn('调用componentWillUpdate');
}

// 8. 组件刷新之后
componentDidUpdate(prevProps: Readonly<P>, prevState: Readonly<S>, snapshot: SS): void {
    console.warn('调用componentDidUpdate');
}

//9. 组件即将销毁的时候调用, 清楚数据
componentWillUnmount(): void {
    console.warn('调用componentWillUnmount');
}
//10. render出错调用该函数
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
    console.warn('调用componentDidCatch')
}

}
其他资料
ReactNative从零开始笔记1-初始化项目
ReactNative从零开始笔记2-组件的生命周期
ReactNative从零开始笔记3-state(状态)与props(属性)
ReactNative从零开始笔记4-PropTypes使用
ReactNative从零开始笔记5-组件传值(父子组件/兄弟组件)
ReactNative从零开始笔记6-导航页面传值(正传和逆传)

上一篇 下一篇

猜你喜欢

热点阅读