์ปดํฌ๋ํธ import ๋ฐ export ํ๊ธฐ
์ปดํฌ๋ํธ์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ ์ฌ์ฌ์ฉ์ฑ์ผ๋ก ์ปดํฌ๋ํธ๋ฅผ ์กฐํฉํด ๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ์ ์๋ค๋ ๊ฒ์ ๋๋ค. ์ปดํฌ๋ํธ๋ฅผ ์ฌ๋ฌ ๋ฒ ์ค์ฒฉํ๊ฒ ๋๋ฉด ๋ค๋ฅธ ํ์ผ๋ก ๋ถ๋ฆฌํด์ผ ํ๋ ์์ ์ด ์๊น๋๋ค. ์ด๋ ๊ฒ ๋ถ๋ฆฌํ๋ฉด ๋์ค์ ํ์ผ์ ์ฐพ๊ธฐ ๋ ์ฝ๊ณ ์ฌ์ฌ์ฉํ๊ธฐ ํธ๋ฆฌํด์ง๋๋ค.
ํ์ต ๋ด์ฉ
- Root ์ปดํฌ๋ํธ๋
- ์ปดํฌ๋ํธ๋ฅผ import ํ๊ฑฐ๋ export ํ๋ ๋ฐฉ๋ฒ
- ์ธ์ default ๋๋ named imports์ exports๋ฅผ ์ฌ์ฉํ ์ง
- ํ ํ์ผ์์ ์ฌ๋ฌ ์ปดํฌ๋ํธ๋ฅผ import ํ๊ฑฐ๋ export ํ๋ ๋ฐฉ๋ฒ
- ์ฌ๋ฌ ์ปดํฌ๋ํธ๋ฅผ ์ฌ๋ฌ ํ์ผ๋ก ๋ถ๋ฆฌํ๋ ๋ฐฉ๋ฒ
Root ์ปดํฌ๋ํธ๋
์ฒซ ์ปดํฌ๋ํธ์์ ๋ง๋ Profile
์ปดํฌ๋ํธ์ Gallery
์ปดํฌ๋ํธ๋ ์๋์ ๊ฐ์ด ๋ ๋๋ง ๋ฉ๋๋ค.
function Profile() { return ( <img src="https://i.imgur.com/MK3eW3As.jpg" alt="Katherine Johnson" /> ); } export default function Gallery() { return ( <section> <h1>Amazing scientists</h1> <Profile /> <Profile /> <Profile /> </section> ); }
์ด ์์์ ์ปดํฌ๋ํธ๋ค์ ๋ชจ๋ App.js
๋ผ๋ root ์ปดํฌ๋ํธ ํ์ผ์ ์กด์ฌํฉ๋๋ค. ์ค์ ์ ๋ฐ๋ผ root ์ปดํฌ๋ํธ๊ฐ ๋ค๋ฅธ ํ์ผ์ ์์นํ ์๋ ์์ต๋๋ค. Next.js์ฒ๋ผ ํ์ผ ๊ธฐ๋ฐ์ผ๋ก ๋ผ์ฐํ
ํ๋ ํ๋ ์์ํฌ์ผ ๊ฒฝ์ฐ ํ์ด์ง๋ณ๋ก root ์ปดํฌ๋ํธ๊ฐ ๋ค๋ฅผ ์ ์์ต๋๋ค.
์ปดํฌ๋ํธ๋ฅผ import ํ๊ฑฐ๋ export ํ๋ ๋ฐฉ๋ฒ
๋๋ฉ ํ๋ฉด์ ๋ณ๊ฒฝํ๊ฒ ๋์ด ๊ณผํ์๋ค์ด ์๋๋ผ ๊ณผํ์ฑ
์ผ๋ก ๋ณ๊ฒฝ๋๊ฑฐ๋ ํ๋กํ ์ฌ์ง์ ๋ค๋ฅธ ๊ณณ์์ ์ฌ์ฉํ๊ฒ ๋๋ค๋ฉด Gallery
์ปดํฌ๋ํธ์ Profile
์ปดํฌ๋ํธ๋ฅผ root ์ปดํฌ๋ํธ๊ฐ ์๋ ๋ค๋ฅธ ํ์ผ๋ก ์ฎ๊ธฐ๋ ๊ฒ ์ข์ต๋๋ค. ๊ทธ๋ ๊ฒ ๋ณ๊ฒฝํ๋ฉด ์ฌ์ฌ์ฉ์ฑ์ด ๋์์ ธ ์ปดํฌ๋ํธ๋ฅผ ๋ชจ๋๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ปดํฌ๋ํธ๋ฅผ ๋ค๋ฅธ ํ์ผ๋ก ์ด๋ํ๋ ค๋ฉด ์ธ ๊ฐ์ง ๋จ๊ณ๊ฐ ์์ต๋๋ค.
- ์ปดํฌ๋ํธ๋ฅผ ์ถ๊ฐํ JS ํ์ผ์ ์์ฑํฉ๋๋ค.
- ์๋ก ๋ง๋ ํ์ผ์์ ํจ์ ์ปดํฌ๋ํธ๋ฅผ export ํฉ๋๋ค. (default ๋๋ named export ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค)
- ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ํ์ผ์์ import ํฉ๋๋ค. (์ ์ ํ ๋ฐฉ์์ ์ ํํด์ default ๋๋ named๋ก import ํฉ๋๋ค)
์๋ ์์๋ฅผ ๋ณด๋ฉด App.js
ํ์ผ์์ Profile
๊ณผ Gallery
์ปดํฌ๋ํธ๋ฅผ ๋นผ์ ์๋ก์ด Gallery.js
ํ์ผ๋ก ์ฎ๊ฒผ์ต๋๋ค. ์ด์ Gallery
๋ Gallery.js
์์ import ํด์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
import Gallery from './Gallery.js'; export default function App() { return ( <Gallery /> ); }
์ด์ ์ด ์์์์๋ ์ปดํฌ๋ํธ๋ค์ด ๋ ํ์ผ๋ก ๋๋๊ฒ ๋์์ต๋๋ค.
Gallery.js
:Profile
์ปดํฌ๋ํธ๋ฅผ ์ ์ํ๊ณ ํด๋น ํ์ผ์์๋ง ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์ export ๋์ง ์์ต๋๋ค.- Default ๋ฐฉ์์ผ๋ก
Gallery
์ปดํฌ๋ํธ๋ฅผ export ํฉ๋๋ค.
App.js
:- Default ๋ฐฉ์์ผ๋ก
Gallery
๋ฅผGallery.js
๋ก๋ถํฐ import ํฉ๋๋ค. - Root
App
์ปดํฌ๋ํธ๋ฅผ default ๋ฐฉ์์ผ๋ก export ํฉ๋๋ค.
- Default ๋ฐฉ์์ผ๋ก
์์ธํ ์ดํด๋ณด๊ธฐ
๋ณดํต JavaScript์์๋ default์ named export๋ผ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ์ export ํฉ๋๋ค. ์ง๊ธ๊น์ง์ ์์์์๋ default export๋ง ์ฌ์ฉํ์ง๋ง ๋ ๋ฐฉ๋ฒ ๋ค ํ ํ์ผ์์ ์ฌ์ฉํ ์๋ ์์ต๋๋ค. ๋ค๋ง ํ ํ์ผ์์๋ ํ๋์ default export๋ง ์กด์ฌํ ์ ์๊ณ named export๋ ์ฌ๋ฌ ๊ฐ๊ฐ ์กด์ฌํ ์ ์์ต๋๋ค.
Export ํ๋ ๋ฐฉ์์ ๋ฐ๋ผ import ํ๋ ๋ฐฉ์์ด ์ ํด์ ธ ์์ต๋๋ค. Default export๋ก ํ ๊ฐ์ named import๋ก ๊ฐ์ ธ์ค๋ ค๊ณ ํ๋ ค๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค. ์๋ ํ์๋ ๊ฐ๊ฐ์ ๊ฒฝ์ฐ์ ๋ฌธ๋ฒ์ด ์ ๋ฆฌ๋์ด ์์ต๋๋ค.
Syntax | Export ๊ตฌ๋ฌธ | Import ๊ตฌ๋ฌธ |
---|---|---|
Default | export default function Button() {} | import Button from './button.js'; |
Named | export function Button() {} | import { Button } from './button.js'; |
Default import๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ํ๋ค๋ฉด import
๋จ์ด ํ์ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด import Banana from './button.js'
๋ผ๊ณ ์ฐ๋๋ผ๋ ๊ฐ์ default export ๊ฐ์ ๊ฐ์ ธ์ค๊ฒ ๋ฉ๋๋ค. ๋ฐ๋๋ก named import๋ฅผ ์ฌ์ฉํ ๋๋ ์์ชฝ ํ์ผ์์ ์ฌ์ฉํ๊ณ ์ ํ๋ ๊ฐ์ ์ด๋ฆ์ด ๊ฐ์์ผ ํ๊ธฐ ๋๋ฌธ์ named import๋ผ๊ณ ๋ถ๋ฆฝ๋๋ค.
๋ณดํธ์ ์ผ๋ก ํ ํ์ผ์์ ํ๋์ ์ปดํฌ๋ํธ๋ง export ํ ๋ default export ๋ฐฉ์์ ์ฌ์ฉํ๊ณ ์ฌ๋ฌ ์ปดํฌ๋ํธ๋ฅผ export ํ ๊ฒฝ์ฐ์ named export ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค. ์ด๋ค ๋ฐฉ์์ ์ฌ์ฉํ๋ ์ปดํฌ๋ํธ์ ํ์ผ์ ์ด๋ฆ์ ์๋ฏธ ์๊ฒ ๋ช
๋ช
ํ๋ ๊ฒ์ ์ค์ํฉ๋๋ค. export default () => {}
์ฒ๋ผ ์ด๋ฆ ์๋ ์ปดํฌ๋ํธ๋ ๋์ค์ ๋๋ฒ๊น
ํ๊ธฐ ์ด๋ ต๊ธฐ ๋๋ฌธ์ ๊ถ์ฅํ์ง ์์ต๋๋ค.
ํ ํ์ผ์์ ์ฌ๋ฌ ์ปดํฌ๋ํธ๋ฅผ import ํ๊ฑฐ๋ export ํ๋ ๋ฐฉ๋ฒ
์ ์ฒด ๊ฐค๋ฌ๋ฆฌ๊ฐ ์๋๋ผ ํ๋์ Profile
๋ง ์ฌ์ฉํ๊ณ ์ถ์ ๋ Profile
์ปดํฌ๋ํธ๋ง export ํ๋ฉด ๋ฉ๋๋ค. ํ์ง๋ง Gallery.js
ํ์ผ์๋ ์ด๋ฏธ ํ๋์ default export๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ๋ ๊ฐ์ default export๋ฅผ ์ ์ํ ์ ์์ต๋๋ค. ์ด๋ฐ ๊ฒฝ์ฐ ์๋ก์ด ํ์ผ ํ๋๋ฅผ ๋ ์์ฑํด์ default export๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ named export๋ก Profile
์ปดํฌ๋ํธ๋ฅผ export ํ ์ ์์ต๋๋ค. ํ ํ์ผ์์๋ ๋จ ํ๋์ default export๋ง ์ฌ์ฉํ ์ ์์ง๋ง named export๋ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋จผ์ named export ๋ฐฉ์์ ์ฌ์ฉํด์ Gallery.js
ํ์ผ์์ Profile
์ปดํฌ๋ํธ๋ฅผ export ํฉ๋๋ค. (default
ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์์ต๋๋ค)
export function Profile() {
// ...
}
๊ทธ ๋ค์์ named import ๋ฐฉ์์ผ๋ก Gallery.js
ํ์ผ์์ Profile
์ปดํฌ๋ํธ๋ฅผ App.js
ํ์ผ๋ก import ํฉ๋๋ค. (์ค๊ดํธ๋ฅผ ์ฌ์ฉํฉ๋๋ค)
import { Profile } from './Gallery.js';
๋ง์ง๋ง์ผ๋ก <Profile />
์ App
์ปดํฌ๋ํธ์์ ๋ ๋๋งํฉ๋๋ค.
export default function App() {
return <Profile />;
}
์ด์ Gallery.js
์๋ default Gallery
export๋ named Profile
export๋ผ๋ ๋ ๊ฐ์ง์ export๊ฐ ์กด์ฌํฉ๋๋ค. App.js
์์๋ ๋ ์ปดํฌ๋ํธ๋ฅผ import ํด์ ์ฌ์ฉํฉ๋๋ค. ์๋์ ์์์์ <Profile />
๊ณผ <Gallery />
๋ฅผ ๊ต์ฐจํด์ ์ฌ์ฉํด ๋ณด์ธ์.
import Gallery from './Gallery.js'; import { Profile } from './Gallery.js'; export default function App() { return ( <Profile /> ); }
์ด์ default์ named export ๋ฐฉ์ ๋ ๋ค ์ฌ์ฉํ ์ ์๊ฒ ๋์ต๋๋ค.
Gallery.js
:- Named export ๋ฐฉ์์ผ๋ก
Profile
์ด๋ผ๋ ์ด๋ฆ์ ์ปดํฌ๋ํธ๋ฅผ export ํฉ๋๋ค. - Default export ๋ฐฉ์์ผ๋ก
Gallery
์ปดํฌ๋ํธ๋ฅผ export ํฉ๋๋ค.
- Named export ๋ฐฉ์์ผ๋ก
App.js
:Gallery.js
์์ named import ๋ฐฉ์์ผ๋กProfile
์ปดํฌ๋ํธ๋ฅผ import ํฉ๋๋ค.Gallery.js
์์ default import ๋ฐฉ์์ผ๋กGallery
์ปดํฌ๋ํธ๋ฅผ import ํฉ๋๋ค.- Default export ๋ฐฉ์์ผ๋ก
App
์ปดํฌ๋ํธ๋ฅผ export ํฉ๋๋ค.
์์ฝ
์ด ํ์ด์ง์์ ๋ฐฐ์ฐ๊ฒ ๋ ๊ฒ๋ค์ ๋๋ค.
- Root ์ปดํฌ๋ํธ๋ ๋ฌด์์ธ์ง
- ์ปดํฌ๋ํธ๋ฅผ import ํ๊ฑฐ๋ export ํ๋ ๋ฐฉ๋ฒ
- ์ธ์ default ๋๋ named imports์ exports๋ฅผ ์ฌ์ฉํ ์ง
- ํ ํ์ผ์์ ์ฌ๋ฌ ์ปดํฌ๋ํธ๋ฅผ export ํ๋ ๋ฐฉ๋ฒ
์ฑ๋ฆฐ์ง 1 of 1: ์ปดํฌ๋ํธ๋ฅผ ํ ๋จ๊ณ ๋ ๋ถ๋ฆฌํ๊ธฐ
ํ์ฌ Gallery.js
ํ์ผ์ด Profile
๊ณผ Gallery
๋ฅผ ๋ ๋ค export ํด์ ํท๊ฐ๋ฆฌ๊ฒ ํ ์ ์์ต๋๋ค.
Profile.js
ํ์ผ์ ์์ฑํด์ Profile
์ปดํฌ๋ํธ๋ฅผ ํด๋น ํ์ผ๋ก ์ฎ๊ธฐ๊ณ App
์ปดํฌ๋ํธ์์๋ <Profile />
๊ณผ <Gallery />
๋ฅผ ๊ฐ๊ฐ ๋ ๋๋งํ๋๋ก ๋ณ๊ฒฝํฉ๋๋ค.
Default ๋๋ named export๋ฅผ ์ฌ์ฉํด์ Profile
์ export ํ ์ ์์ต๋๋ค. ๋ค๋ง ์ฃผ์ํ ์ ์ ์ฌ์ฉํ export ๋ฐฉ์์ ๋ง๋ import ๋ฌธ๋ฒ์ ์ฌ์ฉํด์ผ ํ๋ค๋ ์ ์
๋๋ค. ์๋ ๋ฌธ๋ฒ ํ๋ ์ deep dive์์ ์ธ์ฉํ์ต๋๋ค.
Syntax | Export ๊ตฌ๋ฌธ | Import ๊ตฌ๋ฌธ |
---|---|---|
Default | export default function Button() {} | import Button from './button.js'; |
Named | export function Button() {} | import { Button } from './button.js'; |
// Move me to Profile.js! export function Profile() { return ( <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" /> ); } export default function Gallery() { return ( <section> <h1>Amazing scientists</h1> <Profile /> <Profile /> <Profile /> </section> ); }
Export ๋ฐฉ์ ์ค ํ๋๋ฅผ ์ฌ์ฉํ์ผ๋ฉด ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก๋ ๋์ํ๊ฒ ์๋ํด ๋ณด์ธ์.