React: Anonymous export default vs. Named export default

时间:2019-04-17 02:12:30

标签: reactjs ecmascript-6

Is there any performance hit, undesired side effects or preferred standard when exporting an anonymous React component versus a named component?

anonymous component export

import React from 'react';

export default ({ title }) => <div>{title}</div>;

named component export

import React from 'react';

const MyComponent = ({ title }) => <div>{title}</div>;

export default MyComponent;

3 个答案:

答案 0 :(得分:0)

There are no performance hits as the default export is actually named during import, i.e.: import MyFancyNamedComponent from './MyComponent'

Named exports are only truly necessary when you are doing non-default exports.

答案 1 :(得分:0)

  

只有在执行非默认导出时,才真正需要命名导出。

我不同意以上

最好使用IntelliSense和/或TypeScript export default MyComponent,因为它可以告诉Intellisense和TypeScript更多信息,并允许您使用IDE中的扩展名自动导入模块。如果不影响性能,则命名的导出是高级语法。

答案 2 :(得分:0)

不会影响性能,但是命名可以提高编辑器的可读性和功能性