<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>浅比较 on ZiYang FrontEnd Interview</title><link>https://fe-interview.pangcy.cn/tags/%E6%B5%85%E6%AF%94%E8%BE%83/</link><description>Recent content in 浅比较 on ZiYang FrontEnd Interview</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 06 Mar 2025 13:07:39 +0800</lastBuildDate><atom:link href="https://fe-interview.pangcy.cn/tags/%E6%B5%85%E6%AF%94%E8%BE%83/index.xml" rel="self" type="application/rss+xml"/><item><title>避免不必要的渲染策略</title><link>https://fe-interview.pangcy.cn/docs/framework/react/react-17/</link><pubDate>Wed, 05 Mar 2025 12:28:17 +0000</pubDate><guid>https://fe-interview.pangcy.cn/docs/framework/react/react-17/</guid><description>&lt;h2 id="考察点分析">考察点分析 &lt;a href="#%e8%80%83%e5%af%9f%e7%82%b9%e5%88%86%e6%9e%90" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h2>&lt;p>本题主要考察以下核心能力维度：&lt;/p>
&lt;ol>
&lt;li>&lt;strong>React性能优化理解&lt;/strong>：评估对React渲染机制及优化策略的掌握程度&lt;/li>
&lt;li>&lt;strong>组件更新机制原理&lt;/strong>：检验对虚拟DOM、组件生命周期、渲染触发条件的理解深度&lt;/li>
&lt;li>&lt;strong>不可变数据实践&lt;/strong>：考察对引用类型数据处理的工程化思维&lt;/li>
&lt;/ol>
&lt;p>具体技术评估点：&lt;/p>
&lt;ul>
&lt;li>类组件生命周期方法&lt;code>shouldComponentUpdate&lt;/code>的实现与限制&lt;/li>
&lt;li>函数组件&lt;code>React.memo&lt;/code>的浅比较机制及高阶组件应用&lt;/li>
&lt;li>&lt;code>PureComponent&lt;/code>与普通组件的行为差异&lt;/li>
&lt;li>浅比较（Shallow Compare）的底层实现原理&lt;/li>
&lt;li>引用类型数据在优化策略中的处理要点&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="技术解析">技术解析 &lt;a href="#%e6%8a%80%e6%9c%af%e8%a7%a3%e6%9e%90" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h2>&lt;h3 id="关键知识点">关键知识点 &lt;a href="#%e5%85%b3%e9%94%ae%e7%9f%a5%e8%af%86%e7%82%b9" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h3>&lt;ol>
&lt;li>&lt;code>shouldComponentUpdate&lt;/code> &amp;gt; &lt;code>PureComponent&lt;/code> &amp;gt; 浅比较原理&lt;/li>
&lt;li>&lt;code>React.memo&lt;/code> &amp;gt; 高阶组件 &amp;gt; 引用稳定性&lt;/li>
&lt;li>不可变数据操作 &amp;gt; 性能优化策略&lt;/li>
&lt;/ol>
&lt;h3 id="原理剖析">原理剖析 &lt;a href="#%e5%8e%9f%e7%90%86%e5%89%96%e6%9e%90" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h3>&lt;p>&lt;strong>浅比较工作机制&lt;/strong>：&lt;/p>
&lt;ul>
&lt;li>对基本类型值进行&lt;code>Object.is&lt;/code>比较&lt;/li>
&lt;li>对对象类型进行引用地址比对&lt;/li>
&lt;li>仅比较props第一层属性（Shallow Compare）&lt;/li>
&lt;li>时间复杂度O(n)，空间复杂度O(1)&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>典型执行流程&lt;/strong>：&lt;/p>



 
 
 

 
 
 
 

 

 &lt;div class="prism-codeblock ">
 &lt;pre id="b4e57a5" class="language- ">
 &lt;code>触发更新 → 执行SCU（或自动浅比较）→ 对比新旧props/state → 
│ 无变化 → 阻止渲染 
└ 有变化 → 继续渲染流程&lt;/code>
 &lt;/pre>
 &lt;/div>
&lt;h3 id="常见误区">常见误区 &lt;a href="#%e5%b8%b8%e8%a7%81%e8%af%af%e5%8c%ba" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h3>&lt;ol>
&lt;li>误认为浅比较会递归对比嵌套对象&lt;/li>
&lt;li>在&lt;code>shouldComponentUpdate&lt;/code>中直接修改state导致死循环&lt;/li>
&lt;li>忽略函数类型prop的引用变化问题&lt;/li>
&lt;li>在渲染函数中动态创建对象导致优化失效&lt;/li>
&lt;/ol>
&lt;hr>
&lt;h2 id="问题解答">问题解答 &lt;a href="#%e9%97%ae%e9%a2%98%e8%a7%a3%e7%ad%94" class="anchor" aria-hidden="true">&lt;i class="material-icons align-middle">link&lt;/i>&lt;/a>&lt;/h2>&lt;p>在React中避免不必要渲染的三种策略：&lt;/p>
&lt;ol>
&lt;li>&lt;strong>shouldComponentUpdate（类组件）&lt;/strong>&lt;br>
手动控制更新决策，通过对比nextProps/nextState与当前值的浅比较返回布尔值。典型实现：&lt;/li>
&lt;/ol>



 
 
 

 
 
 
 

 

 &lt;div class="prism-codeblock ">
 &lt;pre id="1481aa2" class="language-javascript ">
 &lt;code>shouldComponentUpdate(nextProps, nextState) {
 return !shallowEqual(this.props, nextProps) || 
 !shallowEqual(this.state, nextState)
}&lt;/code>
 &lt;/pre>
 &lt;/div>
&lt;ol start="2">
&lt;li>&lt;strong>React.memo（函数组件）&lt;/strong>&lt;br>
高阶组件缓存渲染结果，对props进行浅比较。可自定义比较函数：&lt;/li>
&lt;/ol>



 
 
 

 
 
 
 

 

 &lt;div class="prism-codeblock ">
 &lt;pre id="88a329c" class="language-javascript ">
 &lt;code>const MemoizedComp = React.memo(MyComp, (prev, next) =&amp;gt; {
 return shallowEqual(prev, next)
})&lt;/code>
 &lt;/pre>
 &lt;/div>
&lt;ol start="3">
&lt;li>&lt;strong>PureComponent（类组件）&lt;/strong>&lt;br>
自动实现props和state的浅比较，相当于内置&lt;code>shouldComponentUpdate&lt;/code>的优化版本。继承方式：&lt;/li>
&lt;/ol>



 
 
 

 
 
 
 

 

 &lt;div class="prism-codeblock ">
 &lt;pre id="69b2f87" class="language-javascript ">
 &lt;code>class MyComp extends React.PureComponent {
 // 自动进行浅比较
}&lt;/code>
 &lt;/pre>
 &lt;/div>
&lt;p>&lt;strong>浅比较限制&lt;/strong>：&lt;/p></description></item></channel></rss>