React stateless components

99封情书 提交于 2019-12-11 16:58:56

问题


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.


回答1:


Performance wise there will be no difference. The only concern here is regarding the scoping of the renderItem. Since it is enclosed inside List in your second example, it's availability is limited to the scope of List.

Generally, You would want to make such a component a reusable one. In such a case making it globally accessible makes more sense.



来源:https://stackoverflow.com/questions/48395367/react-stateless-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!