Programing Language/Javascript

[Javascript] default export와 named export

JHeaon ・ 2023. 3. 29. 00:27



πŸ“š import, export 

ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λ‹€λ³΄λ©΄ λ‹€λ₯Έ λͺ¨λ“ˆμ—μ„œ ν•¨μˆ˜λ‚˜ λ³€μˆ˜λ“€μ„ 가져와 ν™œμš©ν•˜λŠ” κ²½μš°κ°€ λ§Žμ€λ°, 이λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” import, export 문을 ν†΅ν•΄μ„œ ν•΄λ‹Ή ν–‰μœ„λ₯Ό μ‹€ν–‰ν•œλ‹€. 

 

κ°„λ‹¨ν•˜κ²Œ 보자면, import은 μžμ‹ μ˜ μŠ€νŽ˜μ΄μŠ€μ— λ‹€λ₯Έ λͺ¨λ“ˆμ˜ 것을 λΆˆλŸ¬μ˜¨λ‹€λŠ” 의미이며, exportλŠ” λ°˜λŒ€λ‘œ λ‹€λ₯Έ λͺ¨λ“ˆμ—κ²Œ ν•¨μˆ˜λ‚˜ 값을 μ „λ‹¬ν•˜λŠ” 의미둜 λ°›μ•„λ“œλ¦¬λ©΄ λœλ‹€.

 

 

πŸ“š Named Export와 Default Export

Export ν•˜λŠ” 방법은 2가지가 μžˆλŠ”λ° 차이점은 λ‹€μŒκ³Ό κ°™λ‹€.

 

  • Default Export
    - ν•œ 파일 λ‚΄μ—μ„œ 단 ν•œκ°œμ˜ λ³€μˆ˜/클래슀/ν•¨μˆ˜λ§Œμ„ Export ν•  수 μžˆλ‹€.
    - from 뒀에 λͺ…μ‹œν•œ νŒŒμΌμ—μ„œ 단 ν•œκ°œμ˜ λͺ¨λ“ˆλ§Œ κ°€μ Έμ˜€κΈ° λ•Œλ¬Έμ— as ν‚€μ›Œλ“œ 없이 μ›ν•˜λŠ”λŒ€λ‘œ 이름을 λ°”κΏ€ 수 μžˆλ‹€.
  • Named Export 
    - ν•œ 파일 λ‚΄μ—μ„œ μ—¬λŸ¬κ°œμ˜ λ³€μˆ˜/클래슀/ν•¨μˆ˜λ₯Ό Export ν•  수 μžˆλ‹€.
    - Importν•  λ•Œ as ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ λ‹€λ₯Έ 이름을 지정할 수 μžˆλ‹€.

 

 

πŸ“˜ Defalut Export 

// export
const MyComponent = () => {}
export default MyComponent;

// import
import MyComponent from "./MyDefaultExport";

 

 

πŸ“˜ Named Export 

// exports 
export const MyComponent = () => {}
export const MyComponent2 = () => {}

// imports
import { MyComponent } from "./MyComponent";
import { MyComponent, MyComponent2 } from "./MyComponent";

 

 

 

 

 

πŸ“š μ–΄λ–€ 것이 더 쒋은가?

상황에 따라 λ‹€λ₯΄κ² μ§€λ§Œ Airbnb JavaSciprt Style Guide μ—μ„œλŠ” default export을 ꢌμž₯ν•˜κ³  μžˆλ‹€. ν•˜μ§€λ§Œ 항상 ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λ‹€ 보면, 항상 μƒν™©λ§ˆλ‹€ λ‹€λ₯΄κΈ° λ•Œλ¬Έμ— 적절히 μ„žμ–΄ μ“°λŠ” νŒλ‹¨μ΄ ν•„μš” ν•  λ“― ν•˜λ‹€. 

 

 

 

 

 


πŸ”— μ°Έκ³  λΈ”λ‘œκ·Έ

 

β˜‘οΈ https://blog.neufund.org/why-we-have-banned-default-exports-and-you-should-do-the-same-d51fdc2cf2ad

 

Why we have banned default exports in Javascript and you should do the same

ES2015 was the most important improvement to Javascript in years. Among many great features it brought brand new module system — Ecma…

blog.neufund.org

β˜‘οΈ https://medium.com/@_diana_lee/default-export%EC%99%80-named-export-%EC%B0%A8%EC%9D%B4%EC%A0%90-38fa5d7f57d4

 

default export와 named export 차이점

λ“€μ–΄κ°€λ©°

medium.com

 

'Programing Language/Javascript'의 λ‹€λ₯ΈκΈ€

  • ν˜„μž¬κΈ€ [Javascript] default export와 named export

κ΄€λ ¨κΈ€