RN组件生命周期

本文详细解析了React Native中组件的生命周期,包括装载、更新和卸载三个阶段,阐述了各个生命周期方法的作用及调用时机,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android 和IOS我们都知道组件都有生命周期,RN的组件依旧不例外;
RN组件的生命周期主要分为3个部分
1.装载组件

2.更新组件

3.卸载组件

3-3-component-lifecycle

方法名作用调用次数
constructor构造函数,初始化需要state1次
componentWillMount控件渲染前触发1次
render渲染控件的方法多次
componentDidMount控件渲染后触发1次
componentWillReceiveProps组件接收到新的props被调用多次
shouldComponentUpdate组件接收到新的props或state时调用多次
componentWillUpdate组件接收到新的props或state时调用,shouldComponentUpdate返回为true时调用多次
componentDidUpdate组件更新后调用多次
componentWillUnmount卸载组件调用1次

具体代码参考:
 

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';

export default class LifecycleComponent extends Component {
    constructor(props) {
        super(props);
        console.log("====constructor:props:" + props);
        this.state = {
            count: 0,
        }
    }

    componentWillMount() {
        console.log("====componentWillMount");
    }

    componentDidMount() {
        console.log("====componentDidMount");
    }

    componentWillReceiveProps(nextProps) {
        console.log("====componentWillReceiveProps nextProps:" + nextProps);
    }

    shouldComponentUpdate(nextProps, nextState) {
        console.log("====shouldComponentUpdate:nextProps:" + nextProps + " nextState:" + nextState);
        return true;
    }

    componentWillUpdate() {
        console.log("====componentWillUpdate");
    }

    componentDidUpdate() {
        console.log("====componentDidUpdate");
    }

    componentWillUnmount() {
        console.log("====componentWillUnmount");
    }

    render() {
        console.log("====render");
        return <View>
            <Text onPress={()=>
                this.setState({
                count:this.state.count+1,
            })
            }>生命周期:{this.state.count}</Text>
        </View>
    }

}

这些代码或许记不住,建议安装插件 https://github.com/virtoolswebplayer/ReactNative-LiveTemplate

安装后会自动提示,打开链接会有安装提示;

运行到ios模拟器,command+d 打开js调试工具

google浏览器会出现这样一个页面

单击菜单,打开浏览器控制台

 

1. 装载组件周期验证

2.卸载组件周期验证
在这里我们通过一个字段动态装载这个带日志的组件,通过字段来卸载和装载效果
.App.js代码如下:

export default class App extends Component<Props> {
    constructor(props) {
        super(props);
        this.state = {
            remove: false,
        }
    }

    render() {
        var view = this.state.remove ? null : <LifecycleComponent name="life"/>;
        var text = this.state.remove ? "装载LifecycleComponent" : "卸载LifecycleComponent";
        return (
            <View style={styles.container}>
                {/* <Text style={styles.welcome}>Welcome to React Native!</Text>
                <Text style={styles.instructions}>To get started, edit App.js</Text>
                <Text style={styles.instructions}>{instructions}</Text>*/}
                <HelloComponent name="xxf2"/>

                {/*<LifecycleComponent name="life"/>*/}
                {view}
                <Text onPress={() => {
                        this.setState({
                            remove: !this.state.remove,
                        });
                }}>{text}</Text>
            </View>
        );
    }
}

卸载效果如下:

3.更新时执行的生命周期

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚洲小炫风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值