Default exports or named exports: Why not both
First, let’s understand what is export, it’s a statement that is used to create and export the function, object, from the given file or module. Then the exported module can be used in the other files with help of import statements. It provides the facility to import only the necessary pieces of code from the other module. While working on a project in budendaitech I got a chance to understand it in brief.
There is two ways of export files –
- Named Export
- Default Export
1) Named Export
Using named export statements can export multiple functions, primitives, or objects from a single module, then import the exported files in the required module. Also, It is a must to use an exported name when you import the file.
For example, the ‘str’ module exported as a named export. In the 2nd image importing the modules, it is a must use curly braces and using export name while importing.
Also can import multiple objects or functions from the module as shown in the given example below.
2) Default Export
Default export, using this can export a single module from the file, an object, or anything else. In the case of default, the export can import files by any name.
In the given example below, It is shown that we have exported the demo function as a default export. Now, in the 2nd image module, we are importing it as a ‘demo’ module from the file as default export and then we export the file by importing in the other file. Now in the 3rd image, we are importing the same function as ‘abc’ which shows that we can import any name.
Named Exports vs Default Exports
implemented by using a named export. On the other hand, if the requirement is to export a single file, so can export it as a default export and import by any name.
In this article, there is an explanation of export, import, and comparisons between them in brief. Both ways are good and can be used in different -2 scenarios. For more knowledge can visit given link clickhere