React从0到1--组件生命周期

1、React 严格定义了组件的生命周期,生命周期可能会经历如下三个过程
装载过程( Mount),也就是把组件第一次在 DOM 树中渲染的过程;
更新过程( Update ),当组件被重新渲染的过程;
卸载过程( Unmount),组件从 DOM 中删除的过程

2、装载过程

我们先来看装载过程,当组件第一次被渲染的时候,依次调用的函数是如下这些:
constructor
getlnitialState
getDefaultProps
componentWillMount
render
componentDidMount

1. constructor
  
Es6中创建一个组件的实例,无状态的React组件不需要实例

1、初始化state

this.state = {
count: props.initValue || 0
}

2、绑定成员的this环境

this.onClickIncButton = this.onClickIncButton.bind(this);
this.onClickDecButton = this.onClickDecButton.bind(this);

3、getlnitialState 和 getDefaultProps
React.createClass 方法创造的组件类才会用到这两种方法,分别用来初始化state和props









猜你喜欢

转载自www.cnblogs.com/lk1186578324/p/9803707.html