美文网首页
React 生命周期函数

React 生命周期函数

作者: 张思学 | 来源:发表于2019-11-09 17:01 被阅读0次

生命周期函数指在某一个时刻组件会自动调用执行的函数


image.png
生命周期图详解
  1. lnitialization 初始化过程 props state
  2. Mounting 挂载
    .1 componentWillMount(){} 在组件即将被挂载到页面的时刻自动执行
    .2 render(){} 页面挂载
    .3 componentDidMount() {} 在组件挂载到页面之后自动被执行
  3. Updation 更新
    props 和 state 共有生命周期函数
    .1 shouldComponentUpdate() {} 组件被更新之前
    .2 componentWillUpdate() {}
    组件被更新之前, 但shouldComponentUpdate 执行后并返回为true才会执行
    .3 render(){} 页面挂载
    .4 componentDidUpdate() {} 组件被更新完成之后 (render执行之后) 他会执行
  4. props
    .1 componentWillReceiveProps() {}
    特殊的! 1.一个组件要从父组件接收参数,2.如果这个组件第一次存在与父组件中不执行 3.如果这个组件之前已经存在与父组件中执行
  5. Unmounting 页面去除
    .1 componentWillUnmount() {} 当这个组件即将被从页面中剔除的时候,会被执行
代码解释
import React, {Component} from 'react'

class Cycle extends Component {
  constructor(props) {
    super(props)
    this.state = ({
      inputValue: ''
    })
    this.inputChange = this.inputChange.bind(this)
    this.inputSubmit = this.inputSubmit.bind(this)
  }

  // 在组件即将被挂载到页面的时刻自动执行
  componentWillMount() {
    console.log('componentWillMount = 1')
  }

  // 页面挂载
  render() {
    console.log('render = 2')
    return (
      <div>
        <input onChange={this.inputChange} value={this.state.inputValue}/>
        <button onClick={this.inputSubmit}>提交</button>
      </div>
    )
  }

  // 在组件挂载到页面之后自动被执行
  componentDidMount() {
    console.log('componentDidMount = 3')
  }

  /* 一个组件要从父组件接收参数
   * 如果这个组件第一次存在与父组件中不执行
   * 如果这个组件之前已经存在与父组件中执行
   */
  componentWillReceiveProps() {
    console.log('组件被更新 componentWillReceiveProps =  0')
  }

  // 组件被更新之前,他会自动被执行
  shouldComponentUpdate() {
    console.log('组件被更新 shouldComponentUpdate =  1')
    return true  //返回true 需要被更新, false 不需要被更新
  }

  // 组件被更新之前,他会自动被执行, 但它在 shouldComponentUpdate 执行之后并返回为true才会执行
  componentWillUpdate() {
    console.log('组件被更新 componentWillUpdate =  2')
  }

  // 组件被更新完成之后 (render执行之后) 他会执行
  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log('组件被更新之后 componentDidUpdate =  3')
  }

  // 当这个组件即将被从页面中剔除的时候,会被执行
  componentWillUnmount() {
    console.log('组件被剔除 componentWillUnmount')
  }


  inputChange(e) {
    const value = e.target.value
    this.setState(() => ({
      inputValue: value
    }))
  }

  inputSubmit(){
    console.log(this.state.inputValue)
  }
}

export default Cycle

虽然与 React 16.8 Hook 的新增特性有所不同但是也需要对此生命周期进行了解; Hook 后续更新

相关文章

  • RN-生命周期函数(新)

    旧版生命周期函数 react 16.4 以后生命周期函数 http://projects.wojtekmaj.pl...

  • React 生命周期函数

    https://reactjs.org/docs/react-component.html React生命周期函数...

  • React的生命周期函数

    一、生命周期函数的定义 在某个时刻自动被调用的函数是生命周期函数 二、React中生命周期函数示意图 三、生命周期...

  • 04.React高级部分(中)

    React的生命周期函数 React生命周期函数的使用场景 这一小节,我们来做两个生命周期相关的常用操作1.如何避...

  • useState

    react-hooks 如果你熟悉 React class 的生命周期函数,你可以把 useEffect Hook...

  • 四:React 进阶三 (生命周期)

    react(一):组件的生命周期 生命周期函数 在组件运行的某个时刻,会被自动执行的一些函数。 React生命周期...

  • React快速上手5-react中的事件和生命周期函数

    这节中我们将主要介绍react应用中的事件和生命周期函数 React事件 在react中,我们不用addEvent...

  • react生命周期函数

    react 生命周期函数介绍[https://www.cnblogs.com/kdcg/p/9182393.htm...

  • 2020-09-11

    REACT生命周期 (JS胖老师课程 ) 生命周期函数指在某一个时刻组件会自动调用执行的函数 REACT生命周期的...

  • React 生命周期

    React 16.4 的生命周期图 早期React生命周期图 从图中,我们看到了一些变化 废弃的三个生命周期函数 ...

网友评论

      本文标题:React 生命周期函数

      本文链接:https://www.haomeiwen.com/subject/qscnbctx.html