React Native的生命周期解析
发布时间:2020-12-15 20:34:34 所属栏目:百科 来源:网络整理
导读:在React Native中使用组件来封装界面模块时,整个界面就是一个大的组件,开发过程就是不断优化和拆分界面组件、构造整个组件树的过程。 ? ? 上张图涵盖了一个组件从创建、运行到销毁的整个过程。大家可以看到,初始化的时候会调用5个函数(按先后顺序)。这5
在React Native中使用组件来封装界面模块时,整个界面就是一个大的组件,开发过程就是不断优化和拆分界面组件、构造整个组件树的过程。 ? ?
上张图涵盖了一个组件从创建、运行到销毁的整个过程。大家可以看到,初始化的时候会调用5个函数(按先后顺序)。这5个函数在整个组件被创建到销毁的过程中只调用一次。初始化完毕后,当组件的props或者state改变都会触发不同的钩子函数,继而引发组件的重新渲染。现在我们把这过程拆开一点一点来分析。
初始化我们先来看初始化,在初始化的过程中,会按顺序调用下面5个函数。 getDefaultProps:组件实例创建前调用,多个实例间共享引用。注意:如果父组件传递过来的Props和你在该函数中定义的Props的key一样,将会被覆盖。
该函数不同于getDefaultProps,在以后的过程中,会再次调用,所以可以将控制控件状态的一些变量放在这里初始化,比如控件上显示的文字,可以通过this.state来获取值,通过this.setState来修改state值。注意:一旦调用了this.setState方法,组件一定会调用render方法,对组件进行再次渲染,不过,React框架会根据DOM的状态自动判断是否需要真正渲染。
componentWillMount:在render前,getInitalState之后调用。仅调用一次,可以用于改变state操作。
render:组件渲染函数,会返回一个Virtual DOM,只允许返回一个最外层容器组件。render函数尽量保持纯净,只渲染组件,不修改状态,不执行副操作(比如计时器)。 componentDidMount:在render渲染之后,React会根据Virtual DOM来生成真实DOM,生成完毕后会调用该函数。
运行中初始化完成之后,组件将会进入到运行中状态,运行中状态我们将会遇到如下几个函数: componentWillReceiveProps(nextProps):props改变(父容器来更改或是redux),将会调用该函数。新的props将会作为参数传递进来,老的props可以根据this.props来获取。我们可以在该函数中对state作一些处理。注意:在该函数中更新state不会引起二次渲染。
销毁销毁阶段只有一个函数,很简单 componentWillUnmount:组件DOM中移除的时候调用。在这里进行一些相关的销毁操作,比如定时器,监听等等。 案例代码import React,{Component} from ‘react‘; import { View,Text,StyleSheet,TouchableOpacity } from ‘react-native‘; import {Actions} from ‘react-native-router-flux‘; import Student from ‘./Student‘; export default class Home extends Component { constructor(props) { super(props); this.state = { clickText: "开始点击按钮",count: 1,detailContent: true } } componentWillMount() { console.log("componentWillMount1111"); } shouldComponentUpdate(nextProps,nextState){ console.log(this.state.detailContent,‘detailContent‘); if (this.state.count !== nextState.count) { console.log("shouldComponentUpdate1111---组件需要更新"); return true; } return false; } componentWillUpdate(){ console.log("componentWillUpdate1111---组件将要更新"); } componentDidUpdate(){ console.log("componentDidUpdate1111---组件更新完毕"); } componentDidMount() { console.log("componentDidMount1111"); } componentWillUnmount() { console.log("componentWillUnmount1111"); } clickButton(){ const { count } = this.state; this.setState({ clickText: "我点击了按钮",count: count + 1,detailContent: false }) } render() { console.log("render1111"); return ( <View style={styles.container}> <Text>欢迎来到首页</Text> <TouchableOpacity onPress={() => Actions.notice()} > <Text>跳转到公告页</Text> </TouchableOpacity> <Text style={{color: ‘blue‘,fontSize: 40}}>{this.state.count}</Text> <TouchableOpacity style={styles.button} onPress={() => this.clickButton()} > <Text>{this.state.clickText}</Text> </TouchableOpacity> <Student detailContent={this.state.detailContent}/> </View> ) } } const styles = StyleSheet.create({ container: { flex: 1,alignItems: "center",justifyContent: "center" },button: { width: 250,height: 60,backgroundColor: ‘red‘,borderRadius: 10,alignItems: ‘center‘,justifyContent: ‘center‘ } }); import React,StyleSheet } from ‘react-native‘; export default class Student extends Component { constructor(props) { super(props); this.state = {} } componentWillMount() { } componentWillReceiveProps(nextProps){ console.log(this.props.detailContent,‘this--->>componentWillReceiveProps‘); console.log(nextProps.detailContent,‘next--->>componentWillReceiveProps‘) } componentDidMount() { } componentWillUnmount() { } render() { return ( <View style={styles.container}> <Text>欢迎HomeDetails</Text> </View> ) } } const styles = StyleSheet.create({ container: { flex: 1,justifyContent: "center" } }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |