Different types of imports
There are two types of import that confuses me. Today is the time to put an end to it.
August 30, 2024
There are two main types of exports: named exports and default exports.
Named Exports
When you use named exports, you can export multiple values from a module. Each value must be imported using its exact name, enclosed in curly bracesΒ {}.
Example of Named Export
javascript
1// somewhere.js2export const ComponentA = () => {3 return <div>Component A</div>;4};56export const ComponentB = () => {7 return <div>Component B</div>;8};
Importing Named Exports
javascript
1// anotherFile.js2import { ComponentA, ComponentB } from './somewhere';
Default Exports
When you use a default export, you can export a single value from a module. This value can be imported without curly braces and can be given any name during import.
Example of Default Export
javascript
1// somewhere.js2const Component = () => {3 return <div>Default Component</div>;4};56export default Component;
Importing Default Exports
javascript
1// anotherFile.js2import Component from './somewhere';
Combining Named and Default Exports
You can also combine named and default exports in the same module.
Example of Combined Exports
javascript
1// somewhere.js2const DefaultComponent = () => {3 return <div>Default Component</div>;4};56const ComponentA = () => {7 return <div>Component A</div>;8};910const ComponentB = () => {11 return <div>Component B</div>;12};1314export default DefaultComponent;15export { ComponentA, ComponentB };
Importing Combined Exports
javascript
1// anotherFile.js2import DefaultComponent, { ComponentA, ComponentB } from './somewhere';