{"componentChunkName":"component---src-templates-main-js","path":"/named-exports-vs-default-exports/","result":{"data":{"site":{"siteMetadata":{"title":"Sagar Hani","author":"Sagar Hani"}},"markdownRemark":{"id":"69d058b0-34fa-5b64-80dc-59627f3c6748","excerpt":"image Named Exports vs Default Exports The  statement is used in javascript to export our modules which can be used in other files with the help of the…","html":"<p><img src=\"https://res.cloudinary.com/sagarhani/image/upload/v1607778433/sagarhani.in/named-vs-default_wkxucz.png\" alt=\"image\"></p>\n<h2>Named Exports vs Default Exports</h2>\n<p>The <code class=\"language-text\">export</code> statement is used in javascript to export our modules which can be used in other files with the help of the <code class=\"language-text\">import</code> statement.</p>\n<p>There are 2 types of exports :</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">- Named exports\n\n- Default exports</code></pre></div>\n<br />\n<p>We can have multiple named exports in a file. Named exports are useful when you want to export multiple things from a file. While importing, the name of the module should be the same as the module name you are exporting.</p>\n<p>On the other hand, default exports let us import the module with any name, it need not be the same name as the module name you are exporting. You can even export anonymous functions (which don't have a name at all). But, you can have only 1 default export in a file.</p>\n<hr>\n<p>So, if there is a use case where you want to export only 1 module from a file, which one would you prefer ?</p>\n<p>I like to use named export always, no matter if I'm exporting 1 module or multiple modules in a file. I prefer to use this <a href=\"https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-default-export.md\">import/no-default-export</a> eslint rule in my projects.</p>\n<br />\n<h3>Why do I prefer named export ? 🤔</h3>\n<p>Firstly, it's easy to find out where all my exported modules are being used in my code base, as in named imports we strictly need to have the same name.</p>\n<p>If I have a named export like this :</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> <span class=\"token function-variable function\">calcualateAverage</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token operator\">...</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// I should import it with same name in someOtherFile.js</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> calcualateAverage <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">\"../utils/mathOperations\"</span>\n<span class=\"token comment\">// or</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> calcualateAverage <span class=\"token keyword\">as</span> myNewCalculateAverageName <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">\"../utils/mathOperations\"</span></code></pre></div>\n<p>I can easily find <code class=\"language-text\">calcualateAverage</code>.</p>\n<br />\n<p>If I had used a default export and imported this elsewhere with some other name like :</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token keyword\">function</span> <span class=\"token function-variable function\">calculateAverage</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token operator\">...</span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span><span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// importing in someOtherFile.js</span>\n<span class=\"token keyword\">import</span> calcAverage <span class=\"token keyword\">from</span> <span class=\"token string\">\"../utils/mathOperations\"</span></code></pre></div>\n<p>In this case, it's hard to find out that calculateAverage function is being used in some files as the imported name is different.</p>\n<p>Especially, in react, it's easy to find where all the common components are used if I export it as a named export.</p>\n<h3>Helps in refactoring 🛠</h3>\n<p>If I'm refactoring the <code class=\"language-text\">calculateAverage</code> function, say, <code class=\"language-text\">calculateAverageForFirst10Items</code>, I would put effort to go and rename all the named imports, or else it will start throwing errors. But, in the case of default export, one may not put effort to refactor the import names in other places as it doesn't throw an error. This will lead to a code smell. You are calculating average only for the first 10 items but your import name says <code class=\"language-text\">calcuateAverage</code>. The function name doesn't precisely convey what exactly it does.</p>\n<p>Hence, named exports are always safe to use, readable, and helps in maintaining large codebases.</p>","frontmatter":{"title":"Named exports vs Default exports","date":"December 12, 2020","description":"My views on named and default exports","image":"https://res.cloudinary.com/sagarhani/image/upload/v1607778433/sagarhani.in/named-vs-default_wkxucz.png"}}},"pageContext":{"slug":"/named-exports-vs-default-exports/","previous":{"fields":{"slug":"/sagarhani-v26.0.0/"},"frontmatter":{"title":"SagarHani v26.0.0","image":"https://res.cloudinary.com/sagarhani/image/upload/v1596531142/sagarhani.in/sagarhani-v26.png"}},"next":{"fields":{"slug":"/dynamic-og-image-generator/"},"frontmatter":{"title":"Dynamic OG Image Generator","image":"https://res.cloudinary.com/sagarhani/image/upload/v1621696014/sagarhani.in/og-image.png"}}}}}