You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

2 years ago
  1. import React from 'react'
  2. class App extends React.Component {
  3. // constructor 是创建组件时,最先执行,初始化的时候只执行一次
  4. // 作用:1.初始化 state 2.创建 ref 3.使用 bind 解决 this 指向问题等
  5. constructor () {
  6. super()
  7. console.log('constructor')
  8. }
  9. state = {
  10. count: 0
  11. }
  12. clickHandler = () => {
  13. this.setState ({
  14. count: this.state.count + 1
  15. })
  16. }
  17. // componentDidMount 是组件挂载(渲染 DOM 渲染)后执行,初始化的时候执行一次 (类似 vue 中的 mounted)
  18. // 作用:1.发送网络请求 2.DOM 操作
  19. componentDidMount () {
  20. console.log('componentDidMount')
  21. }
  22. // render 是每次组件渲染都会触发
  23. // 作用:渲染 UI (注意,不能在里面调用 setState() )
  24. render () {
  25. console.log('render')
  26. return (
  27. <div>
  28. this is div
  29. <button onClick={this.clickHandler}>{this.state.count}</button>
  30. </div>
  31. )
  32. }
  33. }
  34. export default App