dev-resources.site
for different kinds of informations.
Default Exports vs Named Exports
Published at
8/15/2022
Categories
javascript
typescript
export
default
Author
Vladimir Vovk
export default function A() {}
// vs
export function A() {}
I think that both methods are good. But I can see some advantages of named exports.
Named exports will not allow to implicitly change the import name
// No error with default export
import Button from '../components/Text'
// now we have a "Button" component
// which is actually a "Text" component
// TypeScript will throw and error
// if import and export names don't match
import { Button } from '../components/Text'
// we will get an error here
Named exports are easier to re-export and organise
Imagine we have several UI components inside components
folder. Each component in a separate file: components/Header.tsx
, components/Text.tsx
, components/Button.tsx
, etc.
Now to be able to import these components as import { Header, Text, Button } from '../components'
we just need to create the components/index.ts
.
export * from './Header'
export * from './Text'
export * from './Button'
Compare to the default exports where will need to write:
export { default as Header } from './Header'
export { default as Text } from './Text'
export { default as Button } from './Button'
Named exports are shorter to write
export function Component() {}
// or
export const Component = () => {}
Compared to default exports:
export default function Component() {}
// or
const Component = () => {}
export default Component
If you have any questions or comments, please post them below, press 💖 button and happy hacking! 🙌🏻
Credits
Photo by Sergey Sokolov on Unsplash
Articles
12 articles in total
Terminal Animations with Node.js
read article
Starting React Native Project in 2025
read article
Check Disk Usage with Terminal
read article
How to Change iOS Push Notifications Permission Dialog with Expo
read article
Compress, Convert and Trim Videos with Command Line
read article
Learning Flexbox Details by Building a Button in React Native
read article
Starting React Native Project in 2024
read article
Default Exports vs Named Exports
currently reading
React Native Reanimated 2 Layout Animation Example
read article
React Native Conditional Rendering
read article
When to use React.memo and React.useCallback for Performance Optimisation
read article
Starting React Native Project in 2022
read article
Featured ones: