image.png
image.png

1.组件挂载

1.1.componentDidMount

组件挂载页面完毕之后调用,并且只调用一次,一般在这个钩子中可以开启定时器,发送网络请求,订阅消息,例如一些开机动画。

1.2.componentWillUnmount

组件将要卸载时候被调用,这个钩子主要做收尾的工作,关闭定时器,取消订阅消息。
案例

  1. <!DOCTYPE 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>Document</title>
  8. </head>
  9. <body>
  10. <div id="app"></div>
  11. <script src="../js/react.development.js"></script>
  12. <script src="../js/react-dom.development.js"></script>
  13. <script src="../js/babel.min.js"></script>
  14. <script src="../js/prop-types.js"></script>
  15. <script type="text/babel">
  16. class Input extends React.Component {
  17. state = {
  18. opacity: 1
  19. }
  20. handleClick = () => {
  21. ReactDOM.unmountComponentAtNode(document.getElementById("app"))
  22. }
  23. componentDidMount() {
  24. let { opacity } = this.state
  25. this.timer = setInterval(() => {
  26. opacity -= 0.1
  27. if (this.state.opacity <= 0) opacity = 1
  28. this.setState(
  29. { opacity }
  30. )
  31. }, 200)
  32. }
  33. componentWillUnmount() {
  34. clearInterval(this.timer)
  35. }
  36. render() {
  37. return (
  38. <div>
  39. <h1 style={{ opacity: this.state.opacity }}>点击消失固定</h1>
  40. <button onClick={this.handleClick}>确定</button>
  41. </div>
  42. )
  43. }
  44. }
  45. ReactDOM.render(<Input />, document.getElementById("app"))
  46. </script>
  47. </body>
  48. </html>

1.3.render

初始化渲染状态更新之后

1.4.componentWillMount

组件将要挂载,在render函数之前执行

2.setState执行

2.1shouldComponentUpdate

控制组件更新的阀门,如果该函数的返回值时false,则不往下执行生命周期钩子,render函数也无法执行,默认不写是true

2.2componentWillUpdate

组件将要更新的钩子

2.3componentDidUpdate

组件更新完毕的钩子

2.4.componentWillUnmount

组件将要卸载时候被调用
案例

  1. <!DOCTYPE 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>Document</title>
  8. </head>
  9. <body>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <title>Document</title>
  17. </head>
  18. <body>
  19. <div id="app"></div>
  20. <script src="../js/react.development.js"></script>
  21. <script src="../js/react-dom.development.js"></script>
  22. <script src="../js/babel.min.js"></script>
  23. <script src="../js/prop-types.js"></script>
  24. <script type="text/babel">
  25. class Son extends React.Component {
  26. constructor(props) {
  27. console.log("constructor-son")
  28. super(props)
  29. this.state = {
  30. count: 0
  31. }
  32. }
  33. handleClick = () => {
  34. ReactDOM.unmountComponentAtNode(document.getElementById("app"))
  35. }
  36. add = () => {
  37. let { count } = this.state
  38. count += 1
  39. this.setState({
  40. count
  41. })
  42. }
  43. //组件是否允许被更改
  44. shouldComponentUpdate(){
  45. console.log(this)
  46. console.log("shouldComponentUpdate-son")
  47. return true
  48. }
  49. //组件将要更新
  50. componentWillUpdate(){
  51. console.log("componentWillUpdate-son")
  52. }
  53. //组件更新完毕
  54. componentDidUpdate(){
  55. console.log("componentDidUpdate-son")
  56. }
  57. render() {
  58. console.log("render-son")
  59. let { count } = this.state
  60. return (
  61. <div>
  62. <h1 >这个数字是:{count}</h1>
  63. <button onClick={this.handleClick}>确定</button>
  64. <button onClick={this.add}>点击加一</button>
  65. </div>
  66. )
  67. }
  68. }
  69. ReactDOM.render(<Son/>, document.getElementById("app"))
  70. </script>
  71. </body>
  72. </html>
  73. </body>
  74. </html>

3.forceUpdate

强制更新操作,不更改任何状态下的数据,强制更新一下
调用:this.forceUpdate()

3.1componentWillUpdate

组件将要更新的钩子

3.2 render

初始化渲染,状态更新之后

3.3componentDidUpdate

组件更新完毕的钩子

3.4.componentWillUnmount

组件将要卸载时候被调用

  1. <!DOCTYPE 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>Document</title>
  8. </head>
  9. <body>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <title>Document</title>
  17. </head>
  18. <body>
  19. <div id="app"></div>
  20. <script src="../js/react.development.js"></script>
  21. <script src="../js/react-dom.development.js"></script>
  22. <script src="../js/babel.min.js"></script>
  23. <script src="../js/prop-types.js"></script>
  24. <script type="text/babel">
  25. class Son extends React.Component {
  26. constructor(props) {
  27. console.log("constructor-son")
  28. super(props)
  29. this.state = {
  30. count: 0
  31. }
  32. }
  33. //不改变数据状态,强制更新页面
  34. force=()=>{
  35. this.forceUpdate()
  36. }
  37. //组件是否允许被更改
  38. shouldComponentUpdate(){
  39. console.log(this)
  40. console.log("shouldComponentUpdate-son")
  41. return false
  42. }
  43. //组件将要更新
  44. componentWillUpdate(){
  45. console.log("componentWillUpdate-son")
  46. }
  47. //组件更新完毕
  48. componentDidUpdate(){
  49. console.log("componentDidUpdate-son")
  50. }
  51. render() {
  52. console.log("render-son")
  53. let { count } = this.state
  54. return (
  55. <div>
  56. <h1 >这个数字是:{count}</h1>
  57. <button onClick={this.force}>强制更新</button>
  58. </div>
  59. )
  60. }
  61. }
  62. ReactDOM.render(<Son/>, document.getElementById("app"))
  63. </script>
  64. </body>
  65. </html>
  66. </body>
  67. </html>

4.父组件render

4.1 componentWillReceiveProps

子组件将要接受父组件传递的props时调用,这个函数第一次页面渲染不进行调用
一般在接受props的组件中调用,这个钩子执行完,接下来会进行状态改变生命钩子的操作。

  1. <!DOCTYPE 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>Document</title>
  8. </head>
  9. <body>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <title>Document</title>
  17. </head>
  18. <body>
  19. <div id="app"></div>
  20. <script src="../js/react.development.js"></script>
  21. <script src="../js/react-dom.development.js"></script>
  22. <script src="../js/babel.min.js"></script>
  23. <script src="../js/prop-types.js"></script>
  24. <script type="text/babel">
  25. class Son extends React.Component {
  26. //定义标签类型
  27. static propTypes = {
  28. color: PropTypes.string.isRequired
  29. }
  30. //指定默认的标签属性值
  31. static defaultProps = {
  32. color: "white"
  33. }
  34. //子组件将要接受父组件传递的props时调用
  35. componentWillReceiveProps(props) {
  36. console.log(" componentWillReceiveProps-son", props)
  37. }
  38. //组件是否允许被更改
  39. shouldComponentUpdate() {
  40. console.log("shouldComponentUpdate-son")
  41. return true
  42. }
  43. //组件将要更新
  44. componentWillUpdate() {
  45. console.log("componentWillUpdate-son")
  46. }
  47. //初始化渲染状态更新之后
  48. render() {
  49. return (
  50. <div>
  51. <h1>我是子组件,要改变的颜色是:{this.props.color}</h1>
  52. </div>
  53. )
  54. }
  55. //组件更新完毕
  56. componentDidUpdate() {
  57. console.log("componentDidUpdate-son")
  58. }
  59. }
  60. class Father extends React.Component {
  61. state = {
  62. color: "blue"
  63. }
  64. changeColor = () => {
  65. if (this.state.color == "blue") {
  66. this.setState({
  67. color: "red"
  68. })
  69. }
  70. else {
  71. this.setState({
  72. color: "blue"
  73. })
  74. }
  75. }
  76. render() {
  77. return (
  78. <div>
  79. <h1> 我是父组件,改变颜色</h1> <Son color={this.state.color} />
  80. <button onClick={this.changeColor}>修改颜色</button>
  81. </div>
  82. )
  83. }
  84. }
  85. ReactDOM.render(<Father />, document.getElementById("app"))
  86. </script>
  87. </body>
  88. </html>
  89. </body>
  90. </html>

5.总结

image.png