React.Component与React.PureComponent [英] React.Component vs React.PureComponent

查看:79
本文介绍了React.Component与React.PureComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方反应文档指出"React.PureComponent仅对对象进行浅比较",如果状态为深",则建议不要这样做.

The official React docs state that "React.PureComponent's shouldComponentUpdate() only shallowly compares the objects", and advises against this if state is "deep".

鉴于此,在创建React组件时,为什么有人应该偏爱React.PureComponent吗?

Given this, is there any reason why one should prefer React.PureComponent when creating React components?

问题:

  • 使用React.Component会对我们产生影响,我们可以考虑使用React.PureComponent吗?
  • 我猜PureComponent中的shouldComponentUpdate()仅执行浅表比较.如果是这种情况,难道不能将这种方法用于更深层次的比较吗?
  • 此外,React.PureComponentshouldComponentUpdate()会跳过整个组件子树的道具更新"-这是否意味着道具更改会被忽略?
  • is there any performance impact in using React.Component that we may consider going for React.PureComponent?
  • I am guessing shouldComponentUpdate() of PureComponent performs only shallow comparisons. If this is the case, can't said method be used for deeper comparisons?
  • "Furthermore, React.PureComponent's shouldComponentUpdate() skips prop updates for the whole component subtree" - Does this mean that prop changes are ignored?

阅读此

Question arose from reading into this medium blog, if it helps.

推荐答案

React.PureComponentReact.Component之间的主要区别是PureComponent对状态变化进行了浅比较.这意味着在比较标量值时,它会比较它们的值,但是在比较对象时,它只会比较引用.它有助于提高应用程序的性能.

The major difference between React.PureComponent and React.Component is PureComponent does a shallow comparison on state change. It means that when comparing scalar values it compares their values, but when comparing objects it compares only references. It helps to improve the performance of the app.

当您满足以下任何条件时,您应该选择React.PureComponent.

You should go for React.PureComponent when you can satisfy any of the below conditions.

  • 状态/道具应该是不可变的对象
  • 状态/道具不应具有层次结构
  • 数据更改时,您应该致电forceUpdate

如果使用的是React.PureComponent,则应确保所有子组件也是纯净的.

If you are using React.PureComponent you should make sure all child components are also pure.

使用React.component会对性能产生任何影响, 考虑使用React.PureComponent吗?

is there any performance impact in using React.component that we may consider going for React.PureComponent?

是的,它会提高您的应用程序性能(由于比较浅)

Yes, it will increase your app performance (because of shallow comparison)

我猜测Purecomponent的shouldComponentUpdate()仅执行 浅层比较.如果是这种情况,则无法使用上述方法 进行更深入的比较?

I am guessing shouldComponentUpdate() of Purecomponent performs only shallow comparisons . If this is the case can' t the said method used for deeper comparisons?

您猜对了.如果您满足我上面提到的任何条件,就可以使用它.

You guessed it correctly. You could use it if you satisfy any of the conditions I mentioned above.

此外,React.PureComponent的shouldComponentUpdate()会跳过prop 整个组件子树的更新"-这是否意味着该道具 更改会被忽略吗?

"Furthermore, React.PureComponent's shouldComponentUpdate() skips prop updates for the whole component subtree" - Does this mean that prop changes are ignored?

是的,如果在浅层比较中找不到差异,道具更改将被忽略.

Yes, prop changes will be ignored If it couldn't find difference in shallow comparison.

这篇关于React.Component与React.PureComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆