首页 技术 正文
技术 2022年11月15日
0 收藏 352 点赞 3,656 浏览 3189 个字

写在前面:

  阅读了多遍文章之后,自己总结了一个。一遍加强记忆,和日后回顾。

react组件的生命周期

一、实例化(初始化)

var Button = React.createClass({
getInitialState: function() {
return { data: };
},
setNewNumber: function() {
this.setState({ data: this.state.data + })
},
render: function() {
var show = (this.state.data % === );
return (
<div>
<button onClick = {this.setNewNumber}>INCREMENT</button>
{ show && <Content myNumber = {this.state.data}></Content> }
</div>
);
}
});
var Content = React.createClass({
getDefaultProps: function() {
console.log('getDefaultProps');
return { name: 'gdq'};
},
getInitialState: function() {
console.log('getInitialState');
return { time: };
},
componentWillMount: function() {
console.log('componentWillMount')
},
componentDidMount: function() {
console.log('componentDidMount')
},
render: function() {
return (
<div>
<h3>{this.props.myNumber}</h3>
</div>
);
}
});
ReactDOM.render(
<div>
<Button />
</div>,
document.getElementById('example2')
);

运行结果:

react组件的生命周期

  1. 创建阶段:getDefaultProps()

    该阶段主要发生在定义组件类的时候,即调用React.createClass的时候。这个阶段getDefaultProps() 会被调用一次,并缓存起来——在多个类实例之间共享。在组件的任何实例被创建之前,我们(的代码逻辑)不能依赖这里的this.props。这个方法只负责返回一个对象,然后与合并父组件的相应属性挂载到this.props中。

  2.初始化阶段:(每个组件被使用的时候都会有,包括Unmount后重新Mount的组件, 如上面的<Conent />)

    getInitialState() :该方法在组件的一个生命周期中(从初始化挂载到销毁为一个),只会执行一次;负责返回一个对象,成为this.state的初始值

    componentWillMount() :在新实例挂载到DOM,我们的业务逻辑可以写在这个阶段,例如修改 this.state,启动个计时器什么的。

    render() :返回组件的虚拟DOM,等待reactDOM.render()把该组件渲染都真实的DOM中,

       使用规则

         a:只能访问this.porps, this.state,而不能修该

         b:不能操作DOM

         c:只能有一个顶级元素

         d:可以返回null, false, 任何组件

    componentDidMount() :组件成功渲染到真实DOM后开始执行,在该方法中可通过this.getDOMNode()访问到真实的DOM元素。此时已可以使用其他类库来操作这个DOM。(在服务端渲染中,该方法不会被调用)。例如:发送ajax请求。

 二、存在期(组件已经挂载到真实DOM中,主要就是更新操作)

var Button = React.createClass({
getInitialState: function() {
return { data: };
},
setNewNumber: function() {
this.setState({ data: this.state.data + })
},
render: function() {
return (
<div>
<button onClick = {this.setNewNumber}>INCREMENT</button>
<Content myNumber = {this.state.data}></Content>
</div>
);
}
});
var Content = React.createClass({
componentWillReceiveProps: function(newProps) {
console.log('componentWillReceiveProps')
},
shouldComponentUpdate: function(newProps, newState) {
console.log('shouldComponentUpdate');
return true;
},
componentWillUpdate: function(nextProps, nextState) {
console.log('componentWillUpdate');
},
componentDidUpdate: function(prevProps, prevState) {
console.log('componentDidUpdate')
},
render: function() {
return (
<div>
<h3>{this.props.myNumber}</h3>
</div>
);
}
});
ReactDOM.render(
<div>
<Button />
</div>,
document.getElementById('example2')
);

结果:

react组件的生命周期

  1.更新阶段有两种情况触发,其实就是

    a: this.props  (从父组件接收的props发生改变)

    b: this.state (自己的内部状态发生改变。 this.setState({…}),触发)

  2.其实就是a情况比b情况多一个componentWillReceiveProps阶段

    componentWillReceiveProps(newProps) : 参数为新接收的props对象,在此阶段允许我们配合newProps修改this.state(是更新阶段唯一可以修改 this.state 的地方)

    shouldComponentUpdate(nextProps, nextState) : 返回true(默认)/false, 判断是否执行本次更新。若为false,则跳过后面阶段,不执行更新。

        a: 不允许修改props,state(this和next都不允许)

        b: 一般不需要使用,只在项目进入性能瓶颈时,在这里进行性能优化

    componentWillUpdate(nextProps, nextState) :与componentWillMount方法类似,组件上会接收到新的props或者state渲染之前,调用该方法。但是不可以在该方法中更新state和props。  

    render() : 构建虚拟DOM,并返回

    componentDidUpdate() : 组件在真实DOM中渲染后执行

   如使用context,参数中将添加context

三、卸载期(组件从真实DOM中卸载的时候执行)

react组件的生命周期

  comonentWillUnmount() :组件被移除(从真实DOM中卸载)之前被调用,可以用于做一些清理工作,在componentDidMount方法中添加的所有任务都需要在该方法中撤销,比如创建的定时器或添加的事件监听器。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900