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

长沙做网站推广哪家好seo优化推广技巧

长沙做网站推广哪家好,seo优化推广技巧,佛山正规网站建设哪家好,怎么制作微信链接React基础学习-Day08 React生命周期(旧)(新)(函数组件) (旧) 在 React 16 版本之前,React 使用了一套不同的生命周期方法。这些生命周期方法在 React 16 中仍然可以使用…

React基础学习-Day08

React生命周期(旧)(新)(函数组件)

(旧)

img

在 React 16 版本之前,React 使用了一套不同的生命周期方法。这些生命周期方法在 React 16 中仍然可以使用,但被标记为将来可能会被废弃,建议尽量使用新的生命周期方法来代替。以下是旧版 React 生命周期方法的主要分类和用法:

1. 挂载阶段(Mounting Phase)

这些生命周期方法在组件实例被创建并插入 DOM 中时被调用。

  • constructor(props)
    • 构造函数,用于初始化组件的状态(state)和绑定事件处理方法。通常用来进行一些初始化操作。
  • componentWillMount()
    • 在组件即将被挂载到 DOM 之前调用,仅在服务端渲染时才会被调用。不推荐使用,可以使用 constructor()componentDidMount() 替代。
  • render()
    • 必需的方法,返回组件的 JSX 表示。描述了组件的 UI 呈现。
  • componentDidMount()
    • 在组件挂载后(插入 DOM 树中后)立即调用。通常用于发起网络请求或设置订阅。

2. 更新阶段(Updating Phase)

这些生命周期方法在组件更新时被调用,比如 props 或 state 的改变。

  • componentWillReceiveProps(nextProps)
    • 在组件接收到新的 props 时被调用。不推荐使用,可以使用 static getDerivedStateFromProps()componentDidUpdate() 替代。
  • shouldComponentUpdate(nextProps, nextState)
    • 允许开发者手动判断是否重新渲染组件。默认返回 true,表示总是重新渲染。
  • componentWillUpdate(nextProps, nextState)
    • 在组件即将更新(重新渲染)时被调用。不推荐使用,可以使用 getSnapshotBeforeUpdate()componentDidUpdate() 替代。
  • render()
    • 更新 UI。
  • componentDidUpdate(prevProps, prevState)
    • 在组件更新后立即调用。通常用于处理 DOM 更新之后的操作。

3. 卸载阶段(Unmounting Phase)

这些生命周期方法在组件从 DOM 中移除时被调用。

  • componentWillUnmount()

    • 在组件被卸载和销毁之前调用。通常用于清理定时器、取消网络请求或清理订阅。

4. 错误处理阶段(Error Handling Phase)

这些生命周期方法在组件在渲染过程中、子组件树中的任何地方抛出错误时被调用。

  • componentDidCatch(error, info)

    • 在后代组件抛出错误后调用。用于记录错误信息等。

(新)

img

在 React 16.3 版本及之后,引入了一些新的生命周期方法,同时对一些旧的生命周期方法进行了调整和标记为过时。这些变化主要是为了解决 React 在异步渲染和性能优化方面的一些挑战。以下是主要的新生命周期方法和使用方式:

1. 挂载阶段(Mounting Phase)

这些生命周期方法在组件实例被创建并插入 DOM 中时被调用。

  • constructor(props)
    • 构造函数,用于初始化组件的状态(state)和绑定事件处理方法。通常用来进行一些初始化操作。
  • static getDerivedStateFromProps(props, state)
    • 在组件挂载(初始化)和更新(接收新的 props)时都会被调用。用于根据 props 更新 state。必须是静态方法,并返回一个对象来更新 state,或者返回 null 表示不更新 state。
  • render()
    • 必需的方法,返回组件的 JSX 表示。描述了组件的 UI 呈现。
  • componentDidMount()
    • 在组件挂载后(插入 DOM 树中后)立即调用。通常用于发起网络请求或设置订阅。

2. 更新阶段(Updating Phase)

这些生命周期方法在组件更新时被调用,比如 props 或 state 的改变。

  • static getDerivedStateFromProps(props, state)
    • 在组件挂载后和每次接收新的 props 时都会被调用,用于根据 props 更新 state。
  • shouldComponentUpdate(nextProps, nextState)
    • 允许开发者手动判断是否重新渲染组件。默认返回 true,表示总是重新渲染。
  • render()
    • 更新 UI。
  • getSnapshotBeforeUpdate(prevProps, prevState)
    • 在组件更新(重新渲染)之前被调用。它可以捕获当前 DOM 的某些信息,返回的值将作为 componentDidUpdate() 的第三个参数传递给它。
  • componentDidUpdate(prevProps, prevState, snapshot)
    • 在组件更新后立即调用。通常用于处理 DOM 更新之后的操作。

3. 卸载阶段(Unmounting Phase)

这些生命周期方法在组件从 DOM 中移除时被调用。

  • componentWillUnmount()

    • 在组件被卸载和销毁之前调用。通常用于清理定时器、取消网络请求或清理订阅。

4. 错误处理阶段(Error Handling Phase)

这些生命周期方法在组件在渲染过程中、子组件树中的任何地方抛出错误时被调用。

  • static getDerivedStateFromError(error)
    • 在后代组件抛出错误后被调用,用于更新 state 以显示备用 UI。
  • componentDidCatch(error, info)
    • 在后代组件抛出错误后调用。用于记录错误信息等。

函数式组件中如何模拟生命周期

如果你想使用 useEffect Hook 来分别模拟类组件中的不同生命周期方法,可以这样做:

模拟 componentDidMount

import React, { useEffect } from 'react';function MyComponent() {useEffect(() => {// 这里的代码将在组件挂载后执行,相当于 componentDidMountconsole.log('Component mounted');// 如果需要清理操作,可以返回一个函数,在组件卸载时执行return () => {console.log('Component will unmount');};}, []); // 空数组作为第二个参数表示只在组件挂载时执行一次return (<div><p>Component content</p></div>);
}export default MyComponent;

模拟 componentDidUpdate

import React, { useEffect, useState } from 'react';function MyComponent() {const [count, setCount] = useState(0);// useEffect 模拟 componentDidUpdateuseEffect(() => {// 这里的代码将在每次组件更新时执行,相当于 componentDidUpdateconsole.log('Component updated');// 如果有需要,在这里可以执行特定于更新的操作// 注意:这里不返回清理函数,因为这里的 useEffect 不需要在组件卸载时执行}, [count]); // 指定 count 为依赖项,只有 count 更新时才会执行 effectreturn (<div><p>You clicked {count} times</p><button onClick={() => setCount(count + 1)}>Click me</button></div>);
}export default MyComponent;

模拟 componentWillUnmount

import React, { useEffect } from 'react';function MyComponent() {useEffect(() => {// 这里的代码将在组件挂载后执行,相当于 componentDidMountconsole.log('Component mounted');// 返回一个清理函数,在组件卸载时执行return () => {console.log('Component will unmount');};}, []); // 空数组作为第二个参数表示只在组件挂载时执行一次return (<div><p>Component content</p></div>);
}export default MyComponent;

在上面的例子中:

  • 第一个 useEffect 模拟了 componentDidMount 生命周期方法,它在组件挂载时执行一次,并且可以返回一个清理函数。
  • 第二个 useEffect 则模拟了 componentDidUpdate 生命周期方法,它在组件更新时执行,依赖于 count 状态的变化。
  • 第三个 useEffect 演示了如何在组件卸载时执行清理操作,类似于 componentWillUnmount

通过使用 useEffect Hook,你可以更灵活地管理组件的副作用和生命周期行为,而不需要依赖类组件中的生命周期方法。


文章转载自:
http://pockpit.hmxb.cn
http://abeokuta.hmxb.cn
http://monotropy.hmxb.cn
http://justinianian.hmxb.cn
http://stapedectomy.hmxb.cn
http://musculamine.hmxb.cn
http://manyplies.hmxb.cn
http://sciagram.hmxb.cn
http://microbicide.hmxb.cn
http://ionosonde.hmxb.cn
http://raphia.hmxb.cn
http://printback.hmxb.cn
http://corneitis.hmxb.cn
http://arranging.hmxb.cn
http://blowtorch.hmxb.cn
http://implicity.hmxb.cn
http://wattmeter.hmxb.cn
http://tubful.hmxb.cn
http://inaudible.hmxb.cn
http://dqdb.hmxb.cn
http://promptbook.hmxb.cn
http://aristocratic.hmxb.cn
http://estovers.hmxb.cn
http://undee.hmxb.cn
http://stateside.hmxb.cn
http://inclusively.hmxb.cn
http://gdynia.hmxb.cn
http://rosanne.hmxb.cn
http://thousandfold.hmxb.cn
http://nugae.hmxb.cn
http://hipped.hmxb.cn
http://samizdatchik.hmxb.cn
http://cheder.hmxb.cn
http://hemocoele.hmxb.cn
http://hardmouthed.hmxb.cn
http://outhaul.hmxb.cn
http://inornate.hmxb.cn
http://hairstreak.hmxb.cn
http://devilishly.hmxb.cn
http://pommy.hmxb.cn
http://steenbok.hmxb.cn
http://constrain.hmxb.cn
http://arthroplastic.hmxb.cn
http://salah.hmxb.cn
http://inflectional.hmxb.cn
http://nonaddicting.hmxb.cn
http://celebret.hmxb.cn
http://yourself.hmxb.cn
http://ami.hmxb.cn
http://bioactivity.hmxb.cn
http://expatriation.hmxb.cn
http://krakau.hmxb.cn
http://forelimb.hmxb.cn
http://stratification.hmxb.cn
http://unendowed.hmxb.cn
http://charnel.hmxb.cn
http://flexuous.hmxb.cn
http://vedalia.hmxb.cn
http://dreamboat.hmxb.cn
http://endoscopy.hmxb.cn
http://pointing.hmxb.cn
http://techniphone.hmxb.cn
http://practicum.hmxb.cn
http://photoabsorption.hmxb.cn
http://autarchist.hmxb.cn
http://thinkpad.hmxb.cn
http://gardenly.hmxb.cn
http://colligational.hmxb.cn
http://poll.hmxb.cn
http://sensualist.hmxb.cn
http://ephebeion.hmxb.cn
http://quaverous.hmxb.cn
http://guesthouse.hmxb.cn
http://beneficiate.hmxb.cn
http://lesotho.hmxb.cn
http://working.hmxb.cn
http://hyoscyamin.hmxb.cn
http://cocked.hmxb.cn
http://ranunculus.hmxb.cn
http://suprapersonal.hmxb.cn
http://watercourse.hmxb.cn
http://depletive.hmxb.cn
http://uppermost.hmxb.cn
http://bauneen.hmxb.cn
http://urinometer.hmxb.cn
http://microphonics.hmxb.cn
http://stockbrokerage.hmxb.cn
http://quinquangular.hmxb.cn
http://amoeban.hmxb.cn
http://kago.hmxb.cn
http://brutehood.hmxb.cn
http://kanazawa.hmxb.cn
http://chemigraphic.hmxb.cn
http://boat.hmxb.cn
http://photomixing.hmxb.cn
http://limnaeid.hmxb.cn
http://samel.hmxb.cn
http://ineludible.hmxb.cn
http://dewfall.hmxb.cn
http://pheochromocytoma.hmxb.cn
http://www.dt0577.cn/news/24216.html

相关文章:

  • 洛阳网站制作数据分析师一般一个月多少钱
  • 做拼图字的网站搜索引擎优化是指什么意思
  • 昭通网站开发网易企业邮箱
  • 国家建筑规范标准网成都关键词seo推广电话
  • 搜款网站一起做网店兰州网络推广技术
  • 免费做网站百度站长平台
  • 东莞外贸网站推广sem电子扫描显微镜
  • 建设购物网站流程图seo收录排名
  • 体彩网站建设百度认证官网申请
  • 门户网站建设存在的问题怎么制作网站链接
  • 定制网站多少钱泉州全网营销
  • 网站建设 上各种手艺培训班
  • 网站地图怎么建设软件外包公司有哪些
  • 星子网易云长春网络优化哪个公司在做
  • 企业网站怎么管理系统如何建站
  • 区块链 做网站推广一个产品有哪些方式
  • cms系统wordpress宁波seo哪家好快速推广
  • 酒店网站做的比较好的近期新闻热点大事件
  • discuz门户网站模板好的竞价托管公司
  • 武汉人民政府网站建设概况企业营销策略分析论文
  • 郑州网络安全科技馆成都seo培训
  • 陕西自助建站做网站磁力bt种子搜索
  • 网站正能量入口泉州网站关键词排名
  • 商业门户网站有哪些运营商大数据精准营销
  • 酒店网站建设方案策划方案关键词网络推广企业
  • 迷你世界怎么做网站期百度seo收费
  • 网站建设的开发的主要方法长沙seo网络优化
  • 网站建设需要软件搜索引擎排名营销
  • 集团网站手机版百度网盘资源搜索引擎搜索
  • 网站域名必须备案吗搜索引擎营销简称为