我是 React 的新手,我想知道什么时候应该使用React 组件,什么时候应该使用React PureComponent?
成分:
import React, { Component } from 'react'
Run Code Online (Sandbox Code Playgroud)
纯组件:
import React, { PureComponent } from 'react'
Run Code Online (Sandbox Code Playgroud)
我可以在任何地方使用 React PureComponent 吗?
或者
使用shouldComponentUpdate并检查并返回不需要的false是否安全
我刚刚读了一篇文章,指出使用纯组件实际上弊大于利。他们建议使用“react-update-if-changed”。这是多少真实?
文章:https : //hackernoon.com/react-purecomponent-thinked-harmful-8155b5c1d4bc
我可以在任何地方使用 React PureComponent 吗?
是的,你可以但是越来越多地尝试使用函数式组件。对于类组件,如果您想实现自己的 shouldComponentUpdate,请保持较小并将其扩展到 PureComponent 或 Component,建议在最小的非复杂(嵌套的深层数组或/和对象)道具更改您的组件需求时执行此操作更新。
使用 shouldComponentUpdate 是否安全?
是的,如果你知道你在做什么,意味着你的实现中的任何缺陷,都可能导致性能问题,比如不必要的组件重新渲染,仅仅因为你的 shouldComponentUpdate 实现返回 true 或更糟,你的组件不会重新渲染当你的 shouldComponentUpdate 由于一些小故障返回 false 时,某些道具会发生变化。
引用的媒体帖子试图出售 react-update-if-changed 包,这在开始时似乎很划算,但是当您意识到
真正的问题陈述是关于性能优化的(请参阅https://reactjs.org/docs/optimizing-performance.html)
如何避免不必要的检查以确定组件可以更新并避免不必要的重新渲染?
使用关于Advanced React Patterns的最后一种方法是优化性能和代码库的最佳方法。为了更好地理解它,请参阅Dumb and Smart Components和Presentational and Container Components。
归档时间: |
|
查看次数: |
2324 次 |
最近记录: |