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

广州哪家网站建设公司好nba排名最新排名

广州哪家网站建设公司好,nba排名最新排名,永久免费个人域名注册,石家庄房产信息网站🌈个人主页: 鑫宝Code 🔥热门专栏: 闲话杂谈| 炫酷HTML | JavaScript基础 ​💫个人格言: "如无必要,勿增实体" 文章目录 React 生命周期完全指南一、生命周期概述二、生命周期的三个阶段2.1 挂载阶段&a…

鑫宝Code

🌈个人主页: 鑫宝Code
🔥热门专栏: 闲话杂谈| 炫酷HTML | JavaScript基础
💫个人格言: "如无必要,勿增实体"


文章目录

  • React 生命周期完全指南
    • 一、生命周期概述
    • 二、生命周期的三个阶段
      • 2.1 挂载阶段(Mounting)
      • 2.2 更新阶段(Updating)
      • 2.3 卸载阶段(Unmounting)
    • 三、常用生命周期方法详解
      • 3.1 constructor(构造函数)
      • 3.2 componentDidMount
      • 3.3 componentDidUpdate
      • 3.4 componentWillUnmount
    • 四、生命周期的最佳实践
      • 4.1 性能优化
      • 4.2 错误处理
    • 五、新旧生命周期的变化
      • 5.1 已废弃的生命周期方法
      • 5.2 新增的生命周期方法
    • 六、Hooks 时代的生命周期
    • 七、总结

React 生命周期完全指南

在这里插入图片描述

一、生命周期概述

React 组件的生命周期是指组件从创建、更新到销毁的整个过程。合理地使用生命周期方法可以让我们更好地控制组件的行为,优化性能,并处理副作用。

二、生命周期的三个阶段

2.1 挂载阶段(Mounting)

组件实例被创建并插入 DOM 的过程

class MyComponent extends React.Component {// 1. 构造函数constructor(props) {super(props);this.state = { count: 0 };console.log('1. constructor');}// 2. 静态方法,很少使用static getDerivedStateFromProps(props, state) {console.log('2. getDerivedStateFromProps');return null;}// 3. 渲染方法render() {console.log('3. render');return <div>{this.state.count}</div>;}// 4. 挂载完成componentDidMount() {console.log('4. componentDidMount');}
}

2.2 更新阶段(Updating)

当组件的 props 或 state 发生变化时触发更新

class MyComponent extends React.Component {// 1. 静态方法static getDerivedStateFromProps(props, state) {return null;}// 2. 是否应该更新shouldComponentUpdate(nextProps, nextState) {return true;}// 3. 渲染render() {return <div>{this.state.count}</div>;}// 4. 获取更新前的快照getSnapshotBeforeUpdate(prevProps, prevState) {return null;}// 5. 更新完成componentDidUpdate(prevProps, prevState, snapshot) {// 处理更新后的操作}
}

2.3 卸载阶段(Unmounting)

组件从 DOM 中移除的过程

class MyComponent extends React.Component {componentWillUnmount() {// 清理工作,比如清除定时器、取消订阅等console.log('组件即将卸载');}
}

三、常用生命周期方法详解

在这里插入图片描述

3.1 constructor(构造函数)

constructor(props) {super(props);// 初始化状态this.state = {count: 0,data: []};// 绑定方法this.handleClick = this.handleClick.bind(this);
}

使用场景:

  • 初始化组件的 state
  • 绑定事件处理方法
  • 不要在这里调用 setState
  • 避免在这里执行副作用操作

3.2 componentDidMount

componentDidMount() {// 发起网络请求fetch('api/data').then(res => res.json()).then(data => {this.setState({ data });});// 添加事件监听window.addEventListener('resize', this.handleResize);// 设置定时器this.timer = setInterval(() => {this.setState(state => ({count: state.count + 1}));}, 1000);
}

使用场景:

  • 发起网络请求
  • DOM 操作
  • 添加订阅
  • 设置定时器

3.3 componentDidUpdate

componentDidUpdate(prevProps, prevState, snapshot) {// 比较 props 变化if (this.props.userID !== prevProps.userID) {this.fetchData(this.props.userID);}// 比较 state 变化if (this.state.count !== prevState.count) {document.title = `点击次数:${this.state.count}`;}
}

使用场景:

  • 对比更新前后的数据
  • 根据条件执行副作用
  • 注意避免无限循环

3.4 componentWillUnmount

componentWillUnmount() {// 清除定时器clearInterval(this.timer);// 移除事件监听window.removeEventListener('resize', this.handleResize);// 取消订阅this.subscription.unsubscribe();
}

使用场景:

  • 清理定时器
  • 取消网络请求
  • 清除事件监听
  • 取消订阅

四、生命周期的最佳实践

4.1 性能优化

class OptimizedComponent extends React.Component {shouldComponentUpdate(nextProps, nextState) {// 只在必要时更新return (this.props.value !== nextProps.value ||this.state.count !== nextState.count);}render() {return (<div><h1>{this.props.value}</h1><p>{this.state.count}</p></div>);}
}

4.2 错误处理

class ErrorBoundary extends React.Component {state = { hasError: false };static getDerivedStateFromError(error) {return { hasError: true };}componentDidCatch(error, errorInfo) {// 记录错误日志console.error('错误信息:', error);console.error('错误详情:', errorInfo);}render() {if (this.state.hasError) {return <h1>出错了!</h1>;}return this.props.children;}
}

五、新旧生命周期的变化

5.1 已废弃的生命周期方法

  • componentWillMount
  • componentWillReceiveProps
  • componentWillUpdate

5.2 新增的生命周期方法

  • getDerivedStateFromProps
  • getSnapshotBeforeUpdate

六、Hooks 时代的生命周期

在这里插入图片描述

function HooksComponent() {// 相当于 constructor 和 componentDidMountconst [count, setCount] = useState(0);// 相当于 componentDidMount 和 componentDidUpdateuseEffect(() => {document.title = `点击次数:${count}`;}, [count]);// 相当于 componentDidMount 和 componentWillUnmountuseEffect(() => {const handler = () => console.log('窗口大小改变');window.addEventListener('resize', handler);// 清理函数return () => {window.removeEventListener('resize', handler);};}, []);return <div>计数:{count}</div>;
}

七、总结

React 生命周期方法为我们提供了在组件不同阶段执行代码的机会。合理使用这些方法可以:

  1. 优化组件性能
  2. 正确处理副作用
  3. 管理组件状态
  4. 避免内存泄漏

在实际开发中,最常用的生命周期方法是:

  • constructor:初始化
  • componentDidMount:副作用处理
  • componentDidUpdate:更新后的操作
  • componentWillUnmount:清理工作

随着 React Hooks 的普及,函数组件正在逐渐取代类组件。但理解生命周期概念对于深入理解 React 的工作原理仍然至关重要。

End

http://www.khdw.cn/news/2679.html

相关文章:

  • 静态网站有后台吗网站关键词如何优化
  • 建筑公司网站平台seo软件简单易排名稳定
  • 如何将图片插入网站唐山seo
  • 微信管理软件哪个最好全能优化大师
  • 南阳医疗网站制作价格免费域名注册
  • 广州海珠建网站全自动精准引流软件
  • 环球经贸网兰州seo新站优化招商
  • 有没有那种帮人做ppt的网站seo是什么意思seo是什么职位
  • 北京最新防控疫情公告抖音seo优化公司
  • 网站开发第几类商标网络营销广告名词解释
  • 网站做支付宝花呗分期hs网站推广
  • 做网站找什么公司好网址如何下载视频
  • 英文版企业网站布局设计百度空间登录入口
  • php和织梦那个做网站好十大外贸电商平台
  • 网站规划主要内容百度词条优化工作
  • 分销渠道管理上海seo优化
  • 什么网站用vue做的短期培训就业学校
  • win7 网站建设免费投放广告的平台
  • iis怎么做ip网站吗徐州seo推广优化
  • 做网站用到的单词如何创建网站站点
  • 东营招标信息网网站优化种类
  • 注册登记网站播放视频速度优化
  • 网站的大小网站排名靠前方法
  • 两个人 b站学网络营销去哪个学校
  • 运城 网站 建设 招聘免费推广方法有哪些
  • 福建参观禁毒展览馆的网站建设十大电商代运营公司
  • 苏州网络推广苏州网站建设app开发需要多少钱
  • 苏州电商网站开发南京seo公司
  • 视觉差网站制作百度pc版网页
  • 威海好的网站建设公司哪家好重庆百度快照优化排名