react native学习笔记7——组件生命周期
每个组件都有自己的生命周期,在其生命周期内,组件经历了初始化-运行-销毁的过程。在运行阶段,每次状态(state)或属性(props)发生变化时,都有对应的组件方法将该变化通知给组件进行渲染刷新(关于state和props的介绍可以看上一节react native学习笔记6——Props和State)。下图是经典的组件生命周期图解(ES6),该图显示了组件在生命周期的各个时期系统调用的方法。 下面我们根据初始化-运行-销毁三个阶段逐步分析组件生命周期的过程。 初始化初始化主要发生在创建组件的时候,在这个阶段中会获取组件的默认属性和初始化组件的状态,修改组件状态,渲染组件。在初始化的过程中,会按顺序调用下面4个函数:
运行中初始化完成之后,组件将会进入到运行中状态,在该阶段中通常会由于与用户发生交互之后、网络请求结果返回、父组件有更新等情况导致组件需要重新渲染更新。运行中阶段可能会执行到如下5个函数:
销毁销毁阶段主要发生组件销亡的时候。
日志跟踪理解组件生命周期为了加深记忆,通过代码实例打印组件生命周期中的被执行的各个函数。 import React,{Component} from 'react';
import {
Text,View,} from 'react-native';
export default class Greeting extends Component {
//初始化获取父组件的
constructor(props) {
super(props);
console.log("getDefaultProps");
this.state = {name: props.name};
console.log("getInitialState");
}
// 在render之前调用
componentWillMount(){
console.log("componentWillMount");
}
componentWillUnMount(){
console.log("componentWillUnMount");
}
componentWillUpdate(nextProps,nextState){
console.log("componentWillUpdate");
}
componentDidUpdate(prevProps,prevState){
console.log("componentDidUpdate");
}
componentWillReceiveProps(nextProps){
console.log("componentWillReceiveProps");
}
shouldComponentUpdate(nextProps,nextState) {
console.log("shouldComponentUpdate");
return true;
}
render() {
console.log("render");
return (
<View style={ {height:40,backgroundColor:"darkgray"}}> <Text>Hello {this.state.name} </Text> </View> ); } changeState(){ console.log("changeState"); this.setState({ name: "React Native" }); } componentDidMount() { console.log("componentDidMount"); this.changeState(); } }
该示例中,在componentDidMount中调用了changeState函数,该函数中通过了setState函数对state进行了设置,这时候就会回调shouldComponentUpdate,如果返回true,则会继续调用componentWillUpdate、render、conponentDidUpdate,之后按返回键退出应用,则会进行销毁操作,回调componentWillUnmount。日志输出为: 2.对于props属性的改变而触发componentWillReceiveProps的情况稍微复杂点,由于组件自己不能修改自己的props属性(上一节有介绍),导致props改变只能通过父组件指定并向子层组件传递。因此这里我们添加一个父组件: export default class LifecycleDemo2 extends Component {
constructor(props) {
super(props);
this.state = {name: "onion"};
}
render() {
return (
<View > <Greeting name ={this.state.name} /> </View> ); } changeState(){ console.log("parent changeState"); this.setState({ name: "xiaofang" }); } componentDidMount() { console.log("parent componentDidMount"); this.changeState(); } }
在父组件的render中使用子组件,并将name属性传给子组件。 <Greeting name ={this.state.name} />
父组件中状态name的初始值是”onion” constructor(props) {
super(props);
this.state = {name: "onion"};
}
而当组件加载完成后,即render执行完成后,在componentDidMount中,通过setState函数设置父组件的state.name。 changeState(){
console.log("changeState");
this.setState({
name: "xiaofang"
});
}
将父组件的state.name改为”xiaofang”,此时会触发父组件的render重新渲染,因而对子组件重新指定props.name的值。 componentDidMount() {
console.log("componentDidMount");
//this.changeState();
}
由于父组件改变了子组件Greeting的props的属性,子组件中会调用componentWillReceiveProps函数。 其完整示例如下: import React,} from 'react-native';
class Greeting extends Component {
constructor(props) {
super(props);
this.state = {name: props.name};
console.log("constructor");
}
componentWillMount(){
// 在render之前调用
console.log("componentWillMount");
}
componentWillUnMount(){
console.log("componentWillUnMount");
}
componentWillUpdate(nextProps,prevState){
console.log("componentDidUpdate");
}
componentWillReceiveProps(nextProps){
this.setState({
name: nextProps.name
});
console.log("componentWillReceiveProps");
}
shouldComponentUpdate(nextProps,nextState) {
console.log("shouldComponentUpdate");
return true;
}
render() {
console.log("render");
return (
<View style={ {height:40,backgroundColor:"darkgray"}}> <Text>Hello {this.state.name} </Text> </View> ); } changeState(){ console.log("changeState"); this.setState({ name: "React Native" }); } componentDidMount() { console.log("componentDidMount"); //this.changeState(); } componentWillUnmount(){ console.log("componentWillUnmount"); } } export default class LifecycleDemo2 extends Component { constructor(props) { super(props); this.state = {name: "onion"}; } render() { return ( <View > <Greeting name ={this.state.name} /> </View> ); } changeState(){ console.log("parent changeState"); this.setState({ name: "xiaofang" }); } componentDidMount() { console.log("parent componentDidMount"); this.changeState(); } }
log日志输出结果如下: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |