title: React 条件渲染 author: Gamehu tags:
产品1.1时,有较大需求的调整,映射到代码则意味着很多功能需要改造或者新增,此时才发现前期由于只求快,代码的可维护性太差,导致要改造模块功能太麻烦,所以就想着边改造边对代码进行重构,一步步提升性能和可维护性。
这个过程我顺便用Lighthouse跑了一下我们的页面,结果挺崩溃的。
我相当不能接受这个结果,遂列出了优化行动项
官方的文章说的很详细,我很舒服。
https://reactjs.org/docs/reconciliation.html
这篇文章虽然老了点但是讲的通俗易懂,编于更深入的理解React的Reconciliation
https://tylermcginnis.com/react-elements-vs-react-components/
然后我们再谈怎么阻止多余的渲染
官方是这么说的:
Preventing Component from Rendering In rare cases you might want a component to hide itself even though it was rendered by another component. To do this return null instead of its render output.
所以最简单的方式就是当不需要某个模块输出是那就return null,从而避免渲染;
因为我们是React+Redux,梳理代码后得出如下有效技巧:
参考: {% blockquote robinwieruch https://www.robinwieruch.de/conditional-rendering-react/ All React Conditional Rendering Techniques %}
{% endblockquote %}
{% blockquote React https://reactjs.org/docs/faq-internals.html Virtual DOM and Internals %}
{% endblockquote %}
{% blockquote React https://reactjs.org/docs/conditional-rendering.html Conditional Rendering %}
{% endblockquote %}