File tree Expand file tree Collapse file tree
react/component/src/lifecycle Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,6 +13,32 @@ React 是一个由虚拟 DOM 渲染成真实 DOM 的过程,这个过程称为
1313 - componentWillUnmount()
1414 - 这个阶段没有对应的 did 方法
1515
16+ ![ image] ( https://user-images.githubusercontent.com/17243165/27114514-c1b0c79a-50f6-11e7-92a3-c671afc26109.png )
17+
18+ 组件在初始化时会触发5个钩子函数:
19+
20+ | id| 钩子函数| 用处|
21+ | -| -| -|
22+ | 1| getDefaultProps()| 设置默认的props,也可以用defaultProps设置组件的默认属性|
23+ | 2| getInitialState()| 在使用es6的class语法时是没有这个钩子函数的,可以直接在constructor中定义this.state。此时可以访问this.props|
24+ | 3| componentWillMount()| 组件初始化时只调用,以后组件更新不调用,整个生命周期只调用一次,此时可以修改state|
25+ | 4| render()| react最重要的步骤,创建虚拟dom,进行diff算法,更新dom树都在此进行。此时就不能更改state了|
26+ | 5| componentDidMount()| 组件渲染之后调用,可以通过this.getDOMNode()获取和操作dom节点,只调用一次|
27+
28+ 在更新时也会触发5个钩子函数:
29+
30+ | id| 钩子函数| 用处|
31+ | -| -| -|
32+ | 6| componentWillReceivePorps(nextProps)| 组件初始化时不调用,组件接受新的props时调用|
33+ | 7| shouldComponentUpdate(nextProps, nextState)| react性能优化非常重要的一环。组件接受新的state或者props时调用,我们可以设置在此对比前后两个props和state是否相同,如果相同则返回false阻止更新,因为相同的属性状态一定会生成相同的dom树,这样就不需要创造新的dom树和旧的dom树进行diff算法对比,节省大量性能,尤其是在dom结构复杂的时候。不过调用this.forceUpdate会跳过此步骤|
34+ | 8| componentWillUpdate(nextProps, nextState)| 组件初始化时不调用,只有在组件将要更新时才调用,此时可以修改state|
35+ | 9| render()| 同上|
36+ | 10| componentDidUpdate()| 组件初始化时不调用,组件更新完成后调用,此时可以获取dom节点。还有一个卸载钩子函数|
37+ |11|componentWillUnmount()|组件将要卸载时调用,一些事件监听和定时器需要在此时清除。
38+
39+
40+ 以上可以看出来react总共有10个周期函数(render重复一次),这个10个函数可以满足我们所有对组件操作的需求,利用的好可以提高开发效率和组件性能
41+
1642## Mounting
1743
1844指首次渲染或者组件从 DOM 中移除后再次重新渲染,后者场景不会执行 getDefaultProps
@@ -166,4 +192,5 @@ ReactDOM.render(<UnmountingComponent/>, document.getElementById('div3'));
166192
167193### 效果预览
168194
169- - [ 组件的生命周期] ( https://wscats.github.io/react-tutorial/react/component/src/lifecycle/lifecycle.html )
195+ - [ 组件的生命周期DEMO1] ( https://wscats.github.io/react-tutorial/react/component/src/lifecycle/lifecycle.html )
196+ - [ 组件的生命周期DEMO2] ( https://wscats.github.io/react-tutorial/react/component/src/lifecycle/生命周期.html )
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+
4+ < head >
5+ < meta charset ="UTF-8 ">
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
8+ < title > Document</ title >
9+ </ head >
10+
11+ < body >
12+ < div id ="demo ">
13+
14+ </ div >
15+ <!--React 核心库-->
16+ < script src ="../../../js/react.js "> </ script >
17+ <!--React 跟 Dom 相关的功能库-->
18+ < script src ="../../../js/react-dom.js "> </ script >
19+ <!--jsx 转换 js 的框架 babel-->
20+ < script src ="../../../js/browser.min.js "> </ script >
21+ <!-- ES6 JSX -->
22+ < script type ="text/babel ">
23+ class Xpeople extends React . Component {
24+ // 构造函数,组件初始化的时候获取props,state
25+ // 第一个生命周期
26+ constructor ( props ) {
27+ super ( props )
28+ // 父传子 老爸给我的
29+ this . props = props ;
30+ // 自己拥有的 就是vue组件里面data Model
31+ this . state = {
32+ "title" :"微信" ,
33+ "inputValue" :""
34+ } ;
35+ }
36+ // 登场前
37+ componentWillMount ( ) {
38+ this . setState ( {
39+ "title" :"支付宝"
40+ } )
41+ }
42+ // render
43+ // 排练
44+
45+ // 正式登场
46+ componentDidMount ( ) {
47+
48+ }
49+ // 只要props和state变化,该生命周期必定触发
50+ shouldComponentUpdate ( ) {
51+ if ( this . state . inputValue . length > 5 ) {
52+ return true
53+ } else {
54+ return false
55+ }
56+ }
57+ changeValue ( ) {
58+ console . log ( this ) ;
59+ this . setState ( {
60+ "title" :"支付宝"
61+ } )
62+ }
63+ getInputValue ( e ) {
64+ console . log ( e . target . value )
65+ this . setState ( {
66+ "inputValue" :e . target . value
67+ } )
68+
69+ }
70+ // view
71+ render ( ) {
72+ return (
73+ < div >
74+ < p >
75+ < input onChange = { this . getInputValue . bind ( this ) } />
76+ { this . state . title }
77+ </ p >
78+ </ div > ) ;
79+ }
80+ }
81+ ReactDOM . render (
82+ < div >
83+ < Xpeople name = "btn" />
84+ </ div > ,
85+ document . querySelector ( "#demo" )
86+ )
87+
88+ </ script >
89+ </ body >
90+
91+ </ html >
You can’t perform that action at this time.
0 commit comments