简体   繁体   English

反应无状态组件

[英]React stateless components

Let's say I have a component for lists rendering and I can do it in two different ways. 假设我有一个用于列表渲染的组件,并且可以通过两种不同的方式来实现。

The first one: 第一个:

const renderItem => item => <li>{item}</li>;

const List = ({ items }) => (
  <ul>
    {items.map(renderItem)}
  </ul>
);

And the second one: 第二个:

const List = ({ items }) => {
  const renderItem => item => <li>{item}</li>;

  return (
    <ul>
      {items.map(renderItem)}
    </ul>
  );
};

What is the difference between these approaches? 这些方法之间有什么区别? I mean performance, renderings count, best practice or anti-pattern, etc. 我的意思是性能,效果图计数,最佳实践或反模式等。

Performance wise there will be no difference. 明智的表现不会有任何区别。 The only concern here is regarding the scoping of the renderItem . 这里唯一需要关注的是renderItem的作用域。 Since it is enclosed inside List in your second example, it's availability is limited to the scope of List . 由于在第二个示例中,它包含在List内,因此其可用性仅限于List的范围。

Generally, You would want to make such a component a reusable one. 通常,您希望使此类组件成为可重用的组件。 In such a case making it globally accessible makes more sense. 在这种情况下,使其可全局访问更有意义。

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

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