When should I use curly braces for ES6 import?

后端 未结 11 925
借酒劲吻你
借酒劲吻你 2020-11-21 09:46

It seems to be obvious, but I found myself a bit confused about when to use curly braces for importing a single module in ES6. For example, in the React-Native project I am

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 10:31

    Dan Abramov answer above explains about the default exports and named exports.

    Which to use?

    Quoting David Herman: ECMAScript 6 favors the single/default export style, and gives the sweetest syntax to importing the default. Importing named exports can and even should be slightly less concise.

    However in TypeScript named export is favored because of refactoring. Example, if you default export a class and rename it, the class name will change only in that file and not in the other references, with named exports class name will be renamed in all the references. Named exports is also preferred for utilities.

    Overall use whatever you prefer.

    Additional

    Default export is actually a named export with name default, so default export can be imported as:

    import {default as Sample} from '../Sample.js';
    

提交回复
热议问题