• 九、react生命周期钩子(旧)


    1. 组件的生命周期
    1.1 理解
    组件从创建到死亡它会经历一些特定的阶段。
    React组件中包含一系列勾子函数(生命周期回调函数), 会在特定的时刻调用。
    我们在定义组件时,会在特定的生命周期回调函数中,做特定的工作。
    1.2 引入案例
    需求:定义组件实现以下功能:

    让指定的文本做显示 / 隐藏的渐变动画
    从完全可见,到彻底消失,耗时2S
    点击“不活了”按钮从界面中卸载组件
     

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>引出生命周期title>
    8. head>
    9. <body>
    10. <div id="app">div>
    11. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin>script>
    12. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin>script>
    13. <script src="https://unpkg.com/babel-standalone@6/babel.min.js">script>
    14. <script src="https://unpkg.com/prop-types@15.6.2/prop-types.js">script>
    15. <script type="text/babel">
    16. //生命周期回调函数
    17. //创建组件
    18. class Life extends React.Component{
    19. state={opacity:1}
    20. death=()=>{
    21. //清除定时器
    22. //clearInterval(this.timer)
    23. //卸载组件
    24. ReactDOM.unmountComponentAtNode(document.getElementById('app'))
    25. }
    26. //组件挂载完毕,组件挂载页面之后调用,只调用一次
    27. componentDidMount(){
    28. this.timer=setInterval(()=>{
    29. //获取原状态
    30. let {opacity}=this.state
    31. //减少0.1
    32. opacity-=0.1
    33. if(opacity<=0) opacity=1
    34. //设置新的透明度
    35. this.setState({opacity:opacity})
    36. },200)
    37. }
    38. //组件将要被卸载时候调用
    39. componentWillUnmount(){
    40. //在这里清除定时器也可以
    41. clearInterval(this.timer)
    42. }
    43. //render调用的时机:初始化渲染与状态更新之后
    44. render(){
    45. return(
    46. <div>
    47. <h2 style={{opacity:this.state.opacity}}>React学不会怎么办?h2>
    48. <button onClick={this.death}>不活了button>
    49. div>
    50. )
    51. }
    52. }
    53. //2.渲染虚拟DOM到页面
    54. ReactDOM.render(<Life/>,document.getElementById('app'))
    55. script>
    56. body>
    57. html>

    1.3 生命周期的三个阶段(旧)

    在这里插入图片描述
     

    1.3.1 初始化阶段
    由ReactDOM.render()触发—初次渲染

    constructor() —— 类组件中的构造函数
    componentWillMount() —— 组件将要挂载 【即将废弃】
    render() —— 挂载组件
    componentDidMount() —— 组件挂载完成 比较常用
    一般在这个钩子中做一些初始化的事,例如:开启定时器、发送网络请求、订阅消息
     

    1.3.2 更新阶段
    【第一种情况】父组件重新render触发

    componentWillReceiveProps() —— 接收属性参数(非首次)【即将废弃】
    然后调用下面的钩子函数

    【第二种情况】由组件内部this.setSate()

    shouldComponentUpdate() —— 组件是否应该被更新(默认返回true)
    然后调用下面的钩子函数

    【第三种情况】强制更新 forceUpdate()

    componentWillUpdate() ——组件将要更新 【即将废弃】
    render() —— 组件更新
    componentDidUpdate() —— 组件完成更新


    1.3.3 卸载组件
    由ReactDOM.unmountComponentAtNode()触发

    componentWillUnmount() —— 组件即将卸载,常用,一般在这个钩子中做一些收尾的事情,例如:关闭定时器、取消订阅消息

    例如:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>ract生命周期(旧)title>
    8. head>
    9. <body>
    10. <div id="app">div>
    11. <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin>script>
    12. <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin>script>
    13. <script src="https://unpkg.com/babel-standalone@6/babel.min.js">script>
    14. <script src="https://unpkg.com/prop-types@15.6.2/prop-types.js">script>
    15. <script type="text/babel">
    16. //生命周期回调函数
    17. /*
    18. 1.3.1 初始化阶段
    19. 由ReactDOM.render()触发—初次渲染
    20. constructor() —— 类组件中的构造函数
    21. componentWillMount() —— 组件将要挂载 【即将废弃】
    22. render() —— 挂载组件
    23. componentDidMount() —— 组件挂载完成 比较常用
    24. 一般在这个钩子中做一些初始化的事,例如:开启定时器、发送网络请求、订阅消息
    25. 1.3.2 更新阶段
    26. 【第一种情况】父组件重新render触发
    27. componentWillReceiveProps() —— 接收属性参数(非首次)【即将废弃】
    28. 然后调用下面的钩子函数
    29. 【第二种情况】由组件内部this.setSate()
    30. shouldComponentUpdate() —— 组件是否应该被更新(默认返回true)
    31. 然后调用下面的钩子函数
    32. 【第三种情况】强制更新 forceUpdate()
    33. componentWillUpdate() ——组件将要更新 【即将废弃】
    34. render() —— 组件更新
    35. componentDidUpdate() —— 组件完成更新
    36. 1.3.3 卸载组件
    37. 由ReactDOM.unmountComponentAtNode()触发
    38. componentWillUnmount() —— 组件即将卸载,常用,一般在这个钩子中做一些收尾的事情,例如:关闭定时器、取消订阅消息
    39. */
    40. //创建组件
    41. class Count extends React.Component{
    42. //构造器
    43. constructor(props){
    44. super(props);
    45. console.log('Count----constructor');
    46. //初始化状态
    47. this.state={count:0}
    48. }
    49. add=()=>{
    50. const {count}=this.state
    51. this.setState({count:count+1})
    52. }
    53. //卸载组件按钮的回调
    54. death=()=>{
    55. ReactDOM.unmountComponentAtNode(document.getElementById('app'))
    56. }
    57. //强制更新按钮的回调
    58. force=()=>{
    59. this.forceUpdate()
    60. }
    61. //组件将要挂载时候调用的钩子
    62. componentWillMount(){
    63. console.log('Count-----componentWillMount');
    64. }
    65. render (){
    66. console.log('Count----render');
    67. const {count}=this.state
    68. return(
    69. <div>
    70. <h2>当前求和为:{count}h2>
    71. <button onClick={this.add}>点我+1button>
    72. <button onClick={this.death}>卸载组件button>
    73. <button onClick={this.force}>不更改任何状态中的数据,强制更新button>
    74. div>
    75. )
    76. }
    77. //组件挂在完毕调用的钩子
    78. componentDidMount(){
    79. console.log('Count-----componentDidMount');
    80. }
    81. //组件将要卸载时候调用
    82. componentWillUnmount(){
    83. console.log('Count----componentWillUnmount');
    84. }
    85. //组件是否应该更新,控制组件更新的阀门,return返回是(true)就更新,否(false)就不更新生命周期到这就结束了
    86. shouldComponentUpdate(){
    87. console.log('Count----shouldComponentUpdate');
    88. return true
    89. }
    90. //组件将要更新时调用
    91. componentWillUpdate(){
    92. console.log('Count----componentWillUpdate');
    93. }
    94. //组件更新完毕之后调用
    95. componentDidUpdate(){
    96. console.log('Count-----componentDidUpdate');
    97. }
    98. }
    99. //父组件render 子组件执行componentWillReceiveProps
    100. //A是父组件,B是子组件
    101. class A extends React.Component{
    102. state={carName:'奔驰'}
    103. changeCar=()=>{
    104. this.setState({carName:'奥拓'})
    105. }
    106. render(){
    107. return(
    108. <div>
    109. <div>我是A组件div>
    110. <button onClick={this.changeCar}>换车button>
    111. <B carName={this.state.carName}/>
    112. div>
    113. )
    114. }
    115. }
    116. //子组件B
    117. class B extends React.Component{
    118. //组件将要接收新的props时候调用
    119. componentWillReceiveProps(props){
    120. console.log('B----componentWillReceiveProps',props)
    121. }
    122. //组件是否应该更新,控制组件更新的阀门,return返回是(true)就更新,否(false)就不更新生命周期到这就结束了
    123. shouldComponentUpdate(){
    124. console.log('Count----shouldComponentUpdate');
    125. return true
    126. }
    127. //组件将要更新时调用
    128. componentWillUpdate(){
    129. console.log('Count----componentWillUpdate');
    130. }
    131. //组件更新完毕之后调用
    132. componentDidUpdate(){
    133. console.log('Count-----componentDidUpdate');
    134. }
    135. render(){
    136. return(
    137. <div>我是B组件,接收到的车是:{this.props.carName}div>
    138. )
    139. }
    140. }
    141. //2.渲染虚拟DOM到页面
    142. ReactDOM.render(<A/>,document.getElementById('app'))
    143. script>
    144. body>
    145. html>

    重要的勾子

    1. render:初始化渲染或更新渲染调用
    2. componentDidMount:开启监听, 发送ajax请求
    3. componentWillUnmount:做一些收尾工作, 如: 清理定时器

  • 相关阅读:
    java设计模式-创建型模式:1-工厂方法模式
    LeetCode刷题day22||235. 二叉搜索树的最近公共祖先&&701.二叉搜索树中的插入操作&&450.删除二叉搜索树中的节点--二叉树
    宝塔安装的TENGINE(NGINX)添加FAIR模块实现自动负载均衡
    matplotlib 中使用中文
    李永乐线代笔记
    leetcode 1047. Remove All Adjacent Duplicates In String(删除相邻的重复字母)
    2022届秋招Java岗高频面试题盘点,老司机也未必全会,真的太卷了
    【Spring】Spring Security学习笔记
    php用token做登录认证
    【力扣每日一题】2023.9.5 从两个数字数组里生成最小数字
  • 原文地址:https://blog.csdn.net/chunxiaqiudong5/article/details/126279474