react生命周期函数

29 篇文章 0 订阅
25 篇文章 1 订阅

和vue一样react也有在某一个时刻会对应执行的生命周期函数。

这里我将react的生命周期分为三大部分

初始化生命周期
compionentWillMount(组件即将被挂载到页面前执行)
render(组件渲染)
componentDidMount(组件挂载到页面上时执行)


props和state更新生命周期
componentWillReceiveProps(props独有的props变化时执行。子组件接收父组件参数,父组件render重新执行那么子组件中的这个生命周期就会执行!)
shouldConpomentUpdate(组件即将更新之前必须要返回一个Booleans当为false不更新视图且没有后面的生命周期,为true更新)
componentWillUpdate(组件更新之前执行并且在shouldComponentUpdate之后)
render
componentDidUpdate(组件更新完成执行)

卸载生命周期
componentWillUnmount(组件卸载时执行)

下面是学习过程的demo每个生命周期打印对应的生命周期的名字。生命周期对于react是非常重要的,要想学好react必须要了解它的生命周期。

父组件
import React, {Component} from 'react';
import Item from './components/item'

class App extends Component{
  constructor () {
    super()
    this.state = {
      inputValue: ''
    }
  }
  InputChange (event) {
    // this.setState({//这是没有用ref
    //   inputValue: event.target.value
    // })
    this.setState({//这是用ref获取dom元素
      inputValue: this.input.value
    },() => {
      console.log('setState函数执行完了才执行(页面更新口才获取dom要在这里)')
    })
  }


  //组件即将被挂载到页面前执行
  componentWillMount () {
    console.log('componentWillMount')
  }
  //组件渲染
  render () {
    console.log('render')
    return (
      <div className="App">
        React App
        <input 
          value={this.state.inputValue} 
          onChange={this.InputChange.bind(this)}
          ref={(n) => {//传入的n为这个input的dom元素
            this.input = n//这里的this指向这个input标签
          }}
        />
        {
          this.state.inputValue === '' ? <Item content={this.state.inputValue}></Item> : ''
        }
      </div>
    );
  }
  //组件挂载到页面上时执行
  componentDidMount () {
    console.log('componentDidMount')
  }
  //子组件拥有props才有的生命周期(父组件没有)
  componentWillReceiveProps () {
    console.log('componentWillReceiveProps')
  }
  //组件即将更新之前必须要返回一个Booleans当为false不更新视图且没有后面的生命周期,为true才更新
  shouldComponentUpdate () {
    console.log('shouldComponentUpdate')
    return true
  }
  //组件更新之前执行并且在shouldComponentUpdate之后
  componentWillUpdate () {
    console.log('componentWillUpdate')
  }
  //组件更新完成时候执行
  componentDidUpdate () {
    console.log('componentDidUpdate')
  }
  //卸载组件时执行
  componentWillUnmount () {
    console.log('componentWillUnmount')
  }
}

export default App;

 

子组件
import React,{Component} from 'react'

class Item extends Component {
  constructor (props) {
    super()
    this.state = {
      
    }
  }
  render () {
    return (
      <div className="Item">
        我是Item组件{this.props.content}
      </div>
    )
  }
  //子组件拥有props才有的生命周期(父组件没有)
  componentWillReceiveProps () {
    console.log('componentWillReceiveProps')
  }
  //卸载组件时执行
  componentWillUnmount () {
    console.log('componentWillUnmount')
  }
}

export default Item

果然前端三大框架中的任意一种学会了再去学其他的就知道该怎么入手。加油!!!

React中的生命周期函数指的是在组件的生命周期中会被自动调用的一些特定函数。以下是React的常见生命周期函数: 1. constructor(props): 组件创建时调用的函数,用于初始化状态和绑定方法。 2. static getDerivedStateFromProps(props, state): 组件创建和更新时调用的静态函数,在render之前调用。它接收props和state作为参数,返回一个对象来更新state,或者返回null。 3. render(): 渲染函数,用于返回组件的JSX结构。 4. componentDidMount(): 组件挂载后调用的函数,通常用于进行DOM操作、网络请求等副作用操作。 5. shouldComponentUpdate(nextProps, nextState): 组件更新前调用的函数,用于判断是否需要重新渲染组件。默认情况下,每次state或props更新都会重新渲染组件。 6. getSnapshotBeforeUpdate(prevProps, prevState): 组件更新前调用的函数,在render之后、组件DOM更新之前调用。它接收prevProps和prevState作为参数,返回一个值,将作为第三个参数传递给componentDidUpdate()函数。 7. componentDidUpdate(prevProps, prevState, snapshot): 组件更新后调用的函数,通常用于进行DOM操作、网络请求等副作用操作。 8. componentWillUnmount(): 组件卸载前调用的函数,用于清理定时器、取消网络请求等操作。 这些生命周期函数在组件的不同阶段会被自动调用,开发者可以在相应的生命周期函数中执行特定的操作,以实现组件的逻辑和交互效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值