简体   繁体   English

不必要的 React.Fragment 性能会很差吗?

[英]Can unnecessary React.Fragment be poor in performance?

Recently, I found that our company's code performance is poor.最近发现我们公司的代码性能很差。 As I reviewed code, there's so many codes like this:当我查看代码时,有很多这样的代码:

condition ? <SomeComponent /> : <></>

They use Fragment as replacement of null.他们使用 Fragment 作为 null 的替代品。 Can this code occur performance problem?此代码会出现性能问题吗?

It's unlikely that the fragments are causing (if any) noticeable performance issues.碎片不太可能导致(如果有的话)明显的性能问题。 As this answer highlights, fragments don't actually create an extra DOM node.正如这个答案所强调的那样,片段实际上并没有创建额外的 DOM 节点。

With that said, the react docs do suggest using null when you don't want a component to render at all, but again I don't think using them would be the root cause of any slowness in your app.话虽如此,当您根本不希望组件呈现时, react 文档确实建议使用null ,但我再次认为使用它们不会是您的应用程序运行缓慢的根本原因。

I'd look at potential unnecessary re-renders, especially in nested child components being re-rendered by parent components with state changes happening.我会看看潜在的不必要的重新渲染,特别是在嵌套子组件被父组件重新渲染时,state 发生了变化。

I'd also definitely a look at the React Profiler and use it to get a sense of what's causing any performance issues.我也肯定会看看React Profiler并使用它来了解导致任何性能问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM