当前位置: 首页 > backend >正文

react-09React生命周期

1.react生命周期(旧版)

1.1react初始挂载时的生命周期

1:构造器-constructor

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

 2:组件将要挂载-componentWillMount

 // 组件将要挂载componentWillMount() {console.log('2:组件将要挂载-componentWillMount');}

3:开始渲染-render

render(){console.log('3:开始渲染-render');const {count} = this.statereturn(<div><h2>当前的和为{count}</h2><button onClick={this.add}>点我+1</button><button onClick={this.death}>卸载DOM</button></div>)}

 4:组件挂载完成-componentDidMount

// 组件挂载完成componentDidMount() {console.log('4:组件挂载完成-componentDidMount');}

 5:组件卸载-componentWillUnmount

// 组件卸载componentWillUnmount() {console.log('5:组件卸载-componentWillUnmount');}

代码实现演示

点击卸载dom后 

 整体代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Hello,React生命周期(旧)</title>
</head><body><!-- 容器 --><div id="test"></div><!-- {/* // 引入 React核心库 */} --><script src="https://unpkg.com/react@16/umd/react.production.min.js"></script><!-- {/* // 引入 react-dom 用于支持 react 操作 DOM */} --><script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script><!-- {/* // 引入 babel:1. ES6 ==> ES5 2. jsx ==> js */} --><script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script><!-- {/* // 引入 JSX 语法 */} --><script type="text/babel">// 1. 创建组件class Count extends React.Component {// 构造器constructor(props) {console.log('1:构造器-constructor');super(props)// 初始化状态this.state = {count: 0}}add=()=>{// 获取原状态const {count} = this.state// 状态更新this.setState({count: count+1})}death=()=>{ReactDOM.unmountComponentAtNode(document.getElementById('test'))}// 组件将要挂载componentWillMount() {console.log('2:组件将要挂载-componentWillMount');}// 组件挂载完成componentDidMount() {console.log('4:组件挂载完成-componentDidMount');}// 组件卸载componentWillUnmount() {console.log('5:组件卸载-componentWillUnmount');}render(){console.log('3:开始渲染-render');const {count} = this.statereturn(<div><h2>当前的和为{count}</h2><button onClick={this.add}>点我+1</button><button onClick={this.death}>卸载DOM</button></div>)}}ReactDOM.render(<Count />,document.getElementById('test'))</script>
</body></html>
 1.2.react更新数据setState

1:shouldComponentUpdate-判断是否需要更新

// 控制组件更新 默认不写此钩子函数 返回true 组件更新shouldComponentUpdate(nextProps, nextState) {   console.log('1:shouldComponentUpdate-判断是否需要更新');return true}

2:componentWillUpdate-组件将要更新

// 组件将要更新componentWillUpdate(nextProps, nextState) {console.log('2:componentWillUpdate-组件将要更新');}

3:开始渲染-render

 render(){console.log('3:开始渲染-render');const {count} = this.statereturn(<div><h2>当前的和为{count}</h2><button onClick={this.add}>点我+1</button><button onClick={this.death}>卸载DOM</button></div>)}

 4:componentDidUpdate-组件更新完成

// 组件更新完成componentDidUpdate(prevProps, prevState) {console.log('4:componentDidUpdate-组件更新完成');}

5:组件卸载-componentWillUnmount

// 组件卸载componentWillUnmount() {console.log('5:组件卸载-componentWillUnmount');}

整体代码 

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Hello,React生命周期(旧)</title>
</head><body><!-- 容器 --><div id="test"></div><!-- {/* // 引入 React核心库 */} --><script src="https://unpkg.com/react@16/umd/react.production.min.js"></script><!-- {/* // 引入 react-dom 用于支持 react 操作 DOM */} --><script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script><!-- {/* // 引入 babel:1. ES6 ==> ES5 2. jsx ==> js */} --><script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script><!-- {/* // 引入 JSX 语法 */} --><script type="text/babel">// 1. 创建组件class Count extends React.Component {state = {count: 0}add=()=>{// 获取原状态const {count} = this.state// 状态更新this.setState({count: count+1})}death=()=>{ReactDOM.unmountComponentAtNode(document.getElementById('test'))}// 组件卸载componentWillUnmount() {console.log('5:组件卸载-componentWillUnmount');}// 控制组件更新 默认不写此钩子函数 返回true 组件更新shouldComponentUpdate(nextProps, nextState) {   console.log('1:shouldComponentUpdate-判断是否需要更新');return true}// 组件将要更新componentWillUpdate(nextProps, nextState) {console.log('2:componentWillUpdate-组件将要更新');}// 组件更新完成componentDidUpdate(prevProps, prevState) {console.log('4:componentDidUpdate-组件更新完成');}render(){console.log('3:开始渲染-render');const {count} = this.statereturn(<div><h2>当前的和为{count}</h2><button onClick={this.add}>点我+1</button><button onClick={this.death}>卸载DOM</button></div>)}}ReactDOM.render(<Count />,document.getElementById('test'))</script>
</body></html>
1.3.forceUpdate声明周期函数强制刷新

forceUpdate--》componentWillUpdate--》render--》componentDidUpdate--》componentWillUnmount

1.4父组件调用子组件render生命周期

componentWillReceiveProps--生命周期钩子函数---父组件调用子组件 第二次渲染,其他的调用子组件的生命周期如setState以后执行生命周期一致。

父组件A

 // 创建组件A--父组件class A extends React.Component {// 初始化状态state={carName:'奥迪'}// 事件changeCarName=()=>{ this.setState({carName: '宝马'})}render() {return (<div><div>A组件</div><button onClick={this.changeCarName}>修改车名</button>{/*将修改的车名传递给子组件--B*/}<B carName={this.state.carName} /></div>)}}

子组件B

 // 创建组件B--子组件class B extends React.Component {// 生命周期钩子函数---父组件调用子组件 第二次渲染componentWillReceiveProps() {console.log('b--componentWillReceiveProps');}render() {return (// B--props 接收父组件传递的props<div>B组件,接收父组件传递的props:{this.props.carName}</div>)}}
1.5 旧版生命周期总结 

        

基础开发中常用的生命周期钩子:

  • componentDidMount:初始化做的事情,开启定时器,发送网络请求,订阅消息。
  • render:渲染结构。
  • componentWillUnmount:收尾,关闭定时器,取消消息。

2.react生命周期(新版17)

componentWillMount,componentWillReceiveProps,componentWillUpdate,在新的react17上中要加前缀UNSAFE。

http://www.xdnf.cn/news/1630.html

相关文章:

  • 解析塔能科技:绿色低碳智慧节能一站式破局之匙
  • 极狐GitLab 如何从 CSV 导入议题?
  • 实时步数统计系统 kafka + spark +redis
  • 4.1 融合架构设计:LLM与Agent的协同工作模型
  • 遨游三防|30200mAh、双露营灯三防平板,见证堆料天花板
  • 多语言笔记系列:使用用户输入
  • Python爬虫爬取图片并存储到MongoDB(注意:仅尝试存储一条空的示例数据到MongoDB,验证MongoDB的联通性)
  • 220V转18V600mA非隔离芯片WT5110
  • 【防火墙 pfsense】1简介
  • Freerots----任务通知
  • Qt本地化 - installTranslator不生效
  • Atlas 800I A2 离线部署 DeepSeek-R1-Distill-Llama-70B
  • 2025年土建施工员考试题库及答案
  • Control Center安卓版:自定义控制中心,提升手机操作体验
  • PostgreSQL 分区表——范围分区SQL实践
  • 【金仓数据库征文】——金仓数据库:国产数据库的卓越之选
  • Docker-高级使用
  • 反射,枚举,lambda表达式
  • 网页版 deepseek 对话问答内容导出为 PDF 文件和 Word 文件的浏览器插件下载安装和使用说明
  • 【axios取消请求】如何在token过期后取消未响应的请求
  • 针对密码学的 EM 侧信道攻击
  • git 操作
  • Golang编程拒绝类型不安全
  • 嵌入式人工智能应用-第三章 opencv操作8 图像特征之 Haar 特征
  • springboot整合redis实现缓存
  • 协作开发攻略:Git全面使用指南 — 第二部分 高级技巧与最佳实践
  • 无标注文本的行业划分(行业分类)算法 —— 无监督或自监督学习
  • 【TensorFlow深度学习框架】从数学原理到工业级应用
  • 硬件工程师面试常见问题(7)
  • 【GIT】github中的仓库如何删除?