react的生命周期函数(超详细)

一、es5版

实例化

  1. 取得默认属性 getDefaultProps 外部传入的props
  2. 初始状态 getInitailState state状态
  3. 即将挂载 componentWillMount
  4. 描画VDOM render

二、旧版本生命周期

在这里插入图片描述
加载阶段

  1. 取得默认属性,初始状态在constructor中完成(运行一次,可读数据,可同步修改state,异步修改state需要setState,setState在实例产生后才可以使用,可以访问到props)

  2. 即将挂载 componentWillMount

  3. 描画VDOM render (创建虚拟dom,进行diff算法,更新dom树)

  4. 组件渲染之后调用 componentDidMount (一般在这个钩子中做一些初始化的事,例如:开启定时器、发送网络请求、订阅消息) 常用
    更新阶段

  5. props改变 componentWillReceiveProps (组件加载时候不调用,组件接受新的props时调用)

  6. shouldComponentUpdate 是否需要更新(return true就会更新dom,false阻止更新)

  7. 描画VDOM render (创建虚拟dom,进行diff算法,更新dom树)

  8. componentDidUpdate 组件数据更新完成后调用

卸载阶段
compoenentWillUnmount (即将卸载,可以做一些组件相关的清理工作,如青春计时器、网络请求等 )常用

组件卸载: unmountComponentAtNode(document.getElementById('root'))

所有子组件挂载完成,才标记着父组件挂载完成,父组件更新,子组件更新,子组件更新,子组件不更新

//创建组件
		class Count extends React.Component{

			//构造器
			constructor(props){
				console.log('Count---constructor');
				super(props)
				//初始化状态
				this.state = {count:0}
			}

			//加1按钮的回调
			add = ()=>{
				//获取原状态
				const {count} = this.state
				//更新状态
				this.setState({count:count+1})
			}

			//卸载组件按钮的回调
			death = ()=>{
				ReactDOM.unmountComponentAtNode(document.getElementById('test'))
			}

			//强制更新按钮的回调
			force = ()=>{
				this.forceUpdate()
			}

			//组件将要挂载的钩子
			componentWillMount(){
				console.log('Count---componentWillMount');
			}

			//组件挂载完毕的钩子
			componentDidMount(){
				console.log('Count---componentDidMount');
			}

			//组件将要卸载的钩子
			componentWillUnmount(){
				console.log('Count---componentWillUnmount');
			}

			//控制组件更新的“阀门”
			shouldComponentUpdate(){
				console.log('Count---shouldComponentUpdate');
				return true
			}

			//组件将要更新的钩子
			componentWillUpdate(){
				console.log('Count---componentWillUpdate');
			}

			//组件更新完毕的钩子
			componentDidUpdate(){
				console.log('Count---componentDidUpdate');
			}

			render(){
				console.log('Count---render');
				const {count} = this.state
				return(
					<div>
						<h2>当前求和为:{count}</h2>
						<button onClick={this.add}>点我+1</button>
						<button onClick={this.death}>卸载组件</button>
						<button onClick={this.force}>不更改任何状态中的数据,强制更新一下</button>
					</div>
				)
			}
		}
		
		//父组件A
		class A extends React.Component{
			//初始化状态
			state = {carName:'奔驰'}

			changeCar = ()=>{
				this.setState({carName:'奥拓'})
			}

			render(){
				return(
					<div>
						<div>我是A组件</div>
						<button onClick={this.changeCar}>换车</button>
						<B carName={this.state.carName}/>
					</div>
				)
			}
		}
		
		//子组件B
		class B extends React.Component{
			//组件将要接收新的props的钩子
			componentWillReceiveProps(props){
				console.log('B---componentWillReceiveProps',props);
			}

			//控制组件更新的“阀门”
			shouldComponentUpdate(){
				console.log('B---shouldComponentUpdate');
				return true
			}
			//组件将要更新的钩子
			componentWillUpdate(){
				console.log('B---componentWillUpdate');
			}

			//组件更新完毕的钩子
			componentDidUpdate(){
				console.log('B---componentDidUpdate');
			}

			render(){
				console.log('B---render');
				return(
					<div>我是B组件,接收到的车是:{this.props.carName}</div>
				)
			}
		}
		
		//渲染组件
		ReactDOM.render(<Count/>,document.getElementById('root'))

三、新版生命周期

在这里插入图片描述

  1. 在React16新的生命周期弃用了compostWillMount、componentWillReceiveProps、componentWillUpdate
  2. 新增了getDerivedStateFromProps、getSonapshotBeforeUpdate来替代弃用的三个钩子函数(compostWillMount、componentWillReceiveProps、componentWillUpdate)

加载阶段

  1. 渲染前 static getDerivedStateFromProps(props,state)

无法访问this
props,state是更新后的
必须返回一个对象,用来更新state或者null 不更新
必须要初始化state
场景:state的值在任何时候都取决于props时

  1. render

必须return jsx|string|number|null
不会直接于浏览器交互:不要操作DOM|和数据

  1. componentDidMount 挂载完成后执行

更新阶段

  1. 渲染前 static getDerivedStateFromProps(props,state)

无法访问this
props,state是更新后的
必须返回一个对象,用来更新state或者null 不更新
必须要初始化state
场景:state的值在任何时候都取决于props时

  1. render

必须return jsx|string|number|null
不会直接于浏览器交互:不要操作DOM|和数据

  1. getSnapshortBeforeUpdate(prevProps,prevState)

组件能在发生更改之前从DOM中捕获一些信息(dom渲染前的状态)
返回 值|null 会给componentDidUpdate
prevProps、prevState 更新前this.props、this.state更新后

销毁阶段
componentWillUnmont组件即将卸载执行

class Count extends React.Component{
			constructor(props){
				console.log('Count---constructor');
				super(props)
				//初始化状态
				this.state = {count:0}
			}

			//加1按钮的回调
			add = ()=>{
				//获取原状态
				const {count} = this.state
				//更新状态
				this.setState({count:count+1})
			}

			//卸载组件按钮的回调
			death = ()=>{
				ReactDOM.unmountComponentAtNode(document.getElementById('test'))
			}

			//强制更新按钮的回调
			force = ()=>{
				this.forceUpdate()
			}
			
			//若state的值在任何时候都取决于props,那么可以使用getDerivedStateFromProps
			static getDerivedStateFromProps(props,state){
				console.log('getDerivedStateFromProps',props,state);
				return null
			}

			//在更新之前获取快照
			getSnapshotBeforeUpdate(){
				console.log('getSnapshotBeforeUpdate');
				return 'atguigu'
			}

			//组件挂载完毕的钩子
			componentDidMount(){
				console.log('Count---componentDidMount');
			}

			//组件将要卸载的钩子
			componentWillUnmount(){
				console.log('Count---componentWillUnmount');
			}

			//控制组件更新的“阀门”
			shouldComponentUpdate(){
				console.log('Count---shouldComponentUpdate');
				return true
			}

			//组件更新完毕的钩子
			componentDidUpdate(preProps,preState,snapshotValue){
				console.log('Count---componentDidUpdate',preProps,preState,snapshotValue);
			}
			
			render(){
				console.log('Count---render');
				const {count} = this.state
				return(
					<div>
						<h2>当前求和为:{count}</h2>
						<button onClick={this.add}>点我+1</button>
						<button onClick={this.death}>卸载组件</button>
						<button onClick={this.force}>不更改任何状态中的数据,强制更新一下</button>
					</div>
				)
			}
		}
		
		//渲染组件
		ReactDOM.render(<Count count={199}/>,document.getElementById('root'))
  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:当渲染过程,生命周期或子组件的构造函数中抛出错误时,会调用如下方法: static getDerivedStateFromProps()componentDidCatch() 常用生命周期 1.render() 。 引用\[2\]:当组件实例被创建并插入DOM时,其生命周期调用顺序如下: constructor()static getDerivedStateFromProps()render()componentDidMount() 组件更新时 。 引用\[3\]:组件渲染之后调用,只调用一次 2.4、生命周期函数代码示例 import React, { Component } from 'react' export default class OldReactComponent extends Component { constructor(props) { super(props) // getDefaultProps:接收初始props // getInitialState:初始化state } state = { } componentWillMount() { // 组件挂载前触发 } render() { return ( <h2>Old React.Component</h2> ) } componentDidMount() { // 组件挂载后触发 } componentWillReceivePorps(nextProps) { // 接收到新的props时触发 } shouldComponentUpdate(nextProps, nextState) { // 组件Props或者state改变时触发,true:更新,false:不更新 return true } componentWillUpdate(nextProps, nextState) { // 组件更新前触发 } componentDidUpdate() { // 组件更新后触发 } componentWillUnmount() { // 组件卸载时触发 } } 3、新的生命周期 3.1、Mounting(加载阶段:涉及4个钩子函数) 。 问题:react函数组件 生命周期 回答: React函数组件的生命周期相对简单,只有两个阶段:Mounting(加载阶段)和Unmounting(卸载阶段)。在Mounting阶段,函数组件会依次执行以下生命周期函数:constructor()、render()和componentDidMount()。constructor()函数用于初始化组件的状态和绑定事件处理函数。render()函数用于渲染组件的UI。componentDidMount()函数在组件被插入DOM后调用,可以进行一些异步操作或订阅事件。在Unmounting阶段,函数组件会执行componentWillUnmount()函数,在组件被卸载前进行一些清理工作,比如取消订阅事件或清除定时器。除了这些生命周期函数函数组件没有其他生命周期函数可用。 #### 引用[.reference_title] - *1* *2* [React生命周期详解](https://blog.csdn.net/weixin_46872121/article/details/126623679)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [关于React组件生命周期函数的详解](https://blog.csdn.net/p445098355/article/details/104667187)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值