<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>React 生命周期 on Last Stand</title><link>https://amatsuzero.github.io/LastStand/tags/react-%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/</link><description>Recent content in React 生命周期 on Last Stand</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Sat, 08 Feb 2025 00:00:00 +0800</lastBuildDate><atom:link href="https://amatsuzero.github.io/LastStand/tags/react-%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/index.xml" rel="self" type="application/rss+xml"/><item><title>React 生命周期</title><link>https://amatsuzero.github.io/LastStand/posts/frontend/react/react-lifecycle/</link><pubDate>Tue, 17 Dec 2024 00:00:00 +0800</pubDate><guid>https://amatsuzero.github.io/LastStand/posts/frontend/react/react-lifecycle/</guid><description>&lt;h2 id="简介"&gt;简介&lt;/h2&gt;
&lt;p&gt;React从v16.3的版本开始， 对生命周期的钩子进行了渐进式的调整，分别废弃和新增了一些生命周期的钩子函数，本文会从以下四点开始讲解：&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;新旧生命周期函数的对比&lt;/li&gt;
&lt;li&gt;分析为什么要废弃旧的钩子函数&lt;/li&gt;
&lt;li&gt;详解新的生命周期使用场景&lt;/li&gt;
&lt;li&gt;实例代码演示并进行总结&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="新旧生命周期对比"&gt;新旧生命周期对比&lt;/h2&gt;
&lt;p&gt;一个完整的React组件生命周期会依次调用如下钩子：&lt;/p&gt;
&lt;h3 id="old-lifecycle"&gt;old lifecycle&lt;/h3&gt;
&lt;p&gt;&lt;img loading="lazy" src="https://amatsuzero.github.io/LastStand/posts/frontend/react/react-lifecycle/image-01.webp"&gt;&lt;/p&gt;
&lt;p&gt;挂载&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;constructor&lt;/li&gt;
&lt;li&gt;componentWillMount&lt;/li&gt;
&lt;li&gt;render&lt;/li&gt;
&lt;li&gt;componentDidMount&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;更新&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;componentWillReceiveProps&lt;/li&gt;
&lt;li&gt;shouldComponentUpdate&lt;/li&gt;
&lt;li&gt;componentWillUpdate&lt;/li&gt;
&lt;li&gt;render&lt;/li&gt;
&lt;li&gt;componentDidUpdate&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;卸载&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;componentWillUnmount&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="new-lifecycle"&gt;new lifecycle&lt;/h3&gt;
&lt;p&gt;&lt;img loading="lazy" src="https://amatsuzero.github.io/LastStand/posts/frontend/react/react-lifecycle/image-02.webp"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;挂载&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;constructor&lt;/li&gt;
&lt;li&gt;getDerivedStateFromProps&lt;/li&gt;
&lt;li&gt;render&lt;/li&gt;
&lt;li&gt;componentDidMount&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;更新&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;getDerivedStateFromProps&lt;/li&gt;
&lt;li&gt;shouldComponentUpdate&lt;/li&gt;
&lt;li&gt;render&lt;/li&gt;
&lt;li&gt;getSnapshotBeforeUpdate&lt;/li&gt;
&lt;li&gt;componentDidUpdate&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;卸载&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;componentWillUnmount&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;从以上生命周期的对比，我们不难看出，React从v16.3开始废弃 &lt;code&gt;componentWillMount&lt;/code&gt; &lt;code&gt;componentWillReceiveProps&lt;/code&gt; &lt;code&gt;componentWillUpdate&lt;/code&gt; 三个钩子函数&lt;/p&gt;
&lt;h2 id="分析废弃原因"&gt;分析废弃原因&lt;/h2&gt;
&lt;p&gt;Facebook花了两年多的时间搞出了React Fiber, 因为在v15的版本，更新过程是同步的，往往一个主线程长时间被占用，会导致页面性能问题&lt;/p&gt;
&lt;p&gt;而 &lt;strong&gt;React Fiber的机制:&lt;/strong&gt; 利用浏览器 &lt;code&gt;requestIdleCallback&lt;/code&gt; 将可中断的任务进行分片处理，每一个小片的运行时间很短，这样唯一的线程就不会被独占&lt;/p&gt;
&lt;p&gt;需要详细了解可以点下面链接 &lt;a href="https://zhuanlan.zhihu.com/p/26027085"&gt;Morgan大佬 - 知乎&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;那么React Fiber会对生命周期带来什么影响吗？&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;因为React Fiber &lt;code&gt;Reconciliation&lt;/code&gt; 这个过程有可能暂停然后继续执行，所以挂载和更新之前的生命周期钩子就有可能不执行或者多次执行；&lt;/p&gt;
&lt;p&gt;目前React为这几个生命周期钩子提供了别名，分别是：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;UNSAFE_componentWillMount&lt;/li&gt;
&lt;li&gt;UNSAFE_componentWillReceiveProps&lt;/li&gt;
&lt;li&gt;UNSAFE_componentWillUpdate&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;React17将只提供别名，取个别名的目的就是恶心你，不让你使用。&lt;/p&gt;</description></item></channel></rss>