โœ๐Ÿป [Code Camp_TIL] 6์ผ์ฐจ: ํด๋” ๊ตฌ์กฐ ์ฒด๊ณ„ํ™”(container / presenter), props, export์™€ export default

code_Jยท2023๋…„ 3์›” 20์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
9/41
post-thumbnail

ํด๋” ๊ตฌ์กฐ ์ฒด๊ณ„ํ™”


์šฐ๋ฆฌ๊ฐ€ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ ํด๋” ๊ตฌ์กฐ๋ฅผ ์ฒด๊ณ„ํ™”ํ•ด์„œ ํŒŒ์ผ์„ ์ž˜ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค. ํด๋” ๊ตฌ์กฐ์˜ ํŒจํ„ด์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๊ทธ ์ค‘ `React`์—์„œ ์“ฐ๋Š” ํŒจํ„ด์€ ํฌ๊ฒŒ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

container / presentational ํŒจํ„ด: JS์™€ HTML(JSX) ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ๋‚˜๋ˆ”. -> ์œ ๋ช…ํ•œ ๊ณ ์ „ ๋ฐฉ์‹
hooks ํŒจํ„ด: use๋กœ ์‹œ์ž‘ํ•˜๋Š” hooks ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋” ๋งŒ๋“ค๊ธฐ -> ์ตœ์‹  ๋ฐฉ์‹
์•„ํ† ๋ฏน(atomic) ํŒจํ„ด: component๋ฅผ ์ข€ ๋” ์ฒด๊ณ„ํ™”


hooks ํŒจํ„ด์ด ์ตœ์‹ ์‹์ด๊ธฐ๋Š” ํ•˜๋‚˜, ์•„์ง๊นŒ์ง€ ๊ณ ์ „ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์—…๋“ค๋„ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค ์•Œ์•„๋‘˜ ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ๊ฒŒ์‹œํŒ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๋งŒ๋“œ๋Š” ๋™์•ˆ์—๋Š” ๊ณ ์ „ ๋ฐฉ์‹์ธ container / presentational ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ์˜ˆ์ •์ด๋‹ค.


container / presentational ํŒจํ„ด

์†Œ์Šค์ฝ”๋“œ๋ฅผ Container(JS) / Presenter(HTML: JSX)๋กœ ๋‚˜๋ˆ„๋Š” ๋ฐฉ์‹์ด๋‹ค. BoardWrite๋ผ๋Š” ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค๊ณ  ํ•  ๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์ด ํŒŒ์ผ์„ ๋‚˜๋ˆˆ๋‹ค.

BoardWrite.container.js
BoardWrite.presenter.js


์œ„์™€ ๊ฐ™์ด ํŒŒ์ผ์„ ๋‚˜๋ˆด์–ด๋„ ์‹คํ–‰๋  ๋•Œ๋Š” ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ ธ์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค. ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์— ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ๋œ๋‹ค!

import๋˜์–ด ๋ถˆ๋ ค๊ฐ€๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž์‹ ์ปดํฌ๋„ŒํŠธ, importํ•˜์—ฌ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋ผ ํ•œ๋‹ค.


์•„๋ž˜์— BoardWrite.container.js์™€ BoardWrite.presenter.js ํŒŒ์ผ์„ ์˜ˆ์‹œ๋กœ ์•Œ์•„๋ณด์ž.

//BoardWrite.container.js

import BoardWrite from "./BoardWrite.presenter"
//BoardWrite.presenter๋ฅผ ๋ถˆ๋Ÿฌ์˜ด.

export default function BoardWrite() {
    const [writer, setWriter] = useState()

    const handleChanceWriter = (event) => {
        const writer = event.target.value
        setWriter(writer)
    }

    return(
        <BoardWriteUI />
      //BoardWrite.presenter.js์˜ BoardWriteUI ํ•จ์ˆ˜ ๋ถˆ๋Ÿฌ์˜ด.
    )
} 

// BoardWrite.presenter.js

export default function BoardWriteUI() {
    return(
        <Wrapper>
            <Title></Title>
            <Writer type="text" placeholder="์ด๋ฆ„" onChange={handleChangeWriter}/>
        </Wrapper>
    )
}

์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ ๋“ค์–ด๊ฐ€์„œ ์šฐ๋ฆฌ ๋ˆˆ์—๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ณด์—ฌ์ง€๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ ํŒŒ์ผ์€ 2๊ฐœ๋กœ ๋‚˜๋ˆ ์ ธ ์žˆ์ง€๋งŒ ์‹คํ–‰ํ•  ๋•Œ์—๋Š” pages์—์„œ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ ธ์„œ ์‹คํ–‰๋œ๋‹ค๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž!

์šฐ๋ฆฌ๊ฐ€ ๋ธŒ๋ผ์šฐ์ €๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š” component๋Š” pages์— ์žˆ๋Š” index.js๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜๋กœ ํ•ฉ์ณ๋‘” container๋ฅผ pages์— ๊ฐ–๊ณ  ์™€์„œ importํ•ด์•ผ ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ๋ฌธ์ œ๊ฐ€ ํ•˜๋‚˜ ๋ฐœ์ƒํ•œ๋‹ค. ์ปดํฌ๋„ŒํŠธ๋ฅผ 2๊ฐœ๋กœ ๋‚˜๋ˆ„๋ฉด์„œ ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์˜ ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง€๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด props์ด๋‹ค.


์ปดํฌ๋„ŒํŠธ๋ผ๋ฆฌ ๋ฐ์ดํ„ฐ ์ฃผ๊ณ ๋ฐ›๊ธฐ: props

props๋Š” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์™€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ฆ‰, ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋ฌผ๋ ค์ฃผ๋Š” ๋ณ€์ˆ˜ ํ˜น์€ ํ•จ์ˆ˜๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค! props๋Š” ๊ฐ์ฒด๋กœ ๋ฌถ์—ฌ์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋„˜์–ด๊ฐ„๋‹ค. ์œ„์—์„œ ๋ดค๋˜ BoardWrite ์˜ˆ์‹œ๋ฅผ ๋‹ค์‹œ ๋ณด์ž.

//BoardWrite.container.js

export default function BoardWrite() {
    const [writer, setWriter] = useState()

    const handleChanceWriter = (event) => {
        const writer = event.target.value
        setWriter(writer)
    }

    return(
        <BoardWriteUI handleChangeWriter={handleChangeWriter}/>
  // props = { handleChangeWriter: handleChangeWriter }

    )
} 

props๋Š” ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ key์™€ value๊ฐ’์„ ๋‹ด๊ณ  ์žˆ๋‹ค.


// BoardWrite.presenter.js

export default function BoardWriteUI(props) {
  // ์†Œ๊ด„ํ˜ธ ์•ˆ์— props ๋„ฃ์–ด์ฃผ๊ธฐ
    return(
        <Wrapper>
            <Title></Title>
            <Writer type="text" placeholder="์ด๋ฆ„" onChange={props.handleChangeWriter}/>
        </Wrapper>
    )
}

container.js ํŒŒ์ผ์—์„œ props ๊ฐ์ฒด์˜ handleChangeWriter๋ผ๋Š” key์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ฉด ๋œ๋‹ค!


๊ทธ๋ ‡๋‹ค๋ฉด ์ž์‹์ด ๋ถ€๋ชจํ•œํ…Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌผ๋ ค์ค„ ์ˆ˜ ์—†์„๊นŒ? ๋ฌผ๋ ค์ค„ ์ˆ˜ ์—†๋‹ค. React์—์„œ๋Š” ๋ถ€๋ชจ ์ž์‹ํ•œํ…Œ ์ค„ ์ˆ˜๋งŒ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„์ด ๋‹จ๋ฐฉํ–ฅ ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์–ด๋–ป๊ฒŒ ๋ณด๋ฉด ์ด๋Ÿฌํ•œ ์ ์ด ๋ถˆํŽธํ•ด ๋ณด์ด์ง€๋งŒ, ์—๋Ÿฌ๋ฅผ ์บ์น˜ํ•˜๊ธฐ ์‰ฝ๊ณ  ๋ณด๊ธฐ์— ๋” ๊น”๋”ํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.


export๋ž‘ export-default ์ฐจ์ด


์šฐ๋ฆฌ๊ฐ€ import๋ฅผ ํ•ด์˜ฌ ๋•Œ ์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ๋ฆฌ๊ณ  ์˜ค๋Š” ๊ฒƒ์ด ์žˆ๋Š”๊ฐ€ ํ•˜๋ฉด, ์ค‘๊ด„ํ˜ธ ์—†์ด ๋ฐ๋ฆฌ๊ณ  ์˜ค๋Š” ๊ฒƒ์ด ์žˆ๋‹ค. ์ค‘๊ด„ํ˜ธ์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€์— ๋”ฐ๋ผ export์™€ export-default๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

import { useMutation, gql } from "@apollo/client";
//export

import BoardWriteUI from "./BoardWrite.presenter";
//export-default

export๋Š” ํ•œ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋‚ด๋ณด๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๊ด„ํ˜ธ ์•ˆ์—์„œ ์–ด๋–ค ๊ฑธ ์„ ํƒํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€์ง€ ์ ์–ด์„œ importํ•˜๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ import๋ฅผ ํ•ด์˜ฌ ๋•Œ exportํ•œ ์ด๋ฆ„์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋‚ด ๋งˆ์Œ๋Œ€๋กœ import ๋’ค์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” { ์›๋ž˜ ์ด๋ฆ„ as ๋ฐ”๊ฟ€ ์ด๋ฆ„ }์œผ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ํ•œ๋ฒˆ์— ๋ฌถ์–ด์„œ import ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, import * as ์ด๋ฆ„ from '๊ฒฝ๋กœ'์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

๋ฐ˜๋ฉด export-default๋Š” ํ•œ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ํ•˜๋‚˜๋งŒ ๋‚ด๋ณด๋‚ด๊ธฐ ๋•Œ๋ฌธ์— import ํ• ๋•Œ ์ค‘๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋‚ด ๋งˆ์Œ๋Œ€๋กœ import ๋’ค ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ’ก ์˜ค๋Š˜์˜ ๋Š๋‚€ ์ : ๊ทธ๋™์•ˆ ๋งŒ๋“œ๋Š” ํŽ˜์ด์ง€์˜ ์ด๋ฆ„๋งŒ ๊ตฌ๋ถ„ํ•ด์„œ ํด๋”๋ฅผ ๋งŒ๋“ค์—ˆ์—ˆ๋Š”๋ฐ, ์˜ค๋Š˜ ํด๋” ๊ตฌ์กฐ ์ฒด๊ณ„ํ™”๋ฅผ ๋ฐฐ์šฐ๋ฉด์„œ ์•ž์œผ๋กœ์˜ ํ”„๋กœ์ ํŠธ์—๋Š” ํด๋”๋ฅผ ์ž˜ ๋‚˜๋ˆ„์–ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ํ›จ์”ฌ ๊ฐ€๋…์„ฑ์ด ๋†’์€ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค๊ณ , ๋ฌธ์ œ์ ์„ ์ฐพ๊ณ  ์ˆ˜์ •ํ•˜๋Š” ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.
๋ฉ˜ํ† ๋‹˜๊ป˜์„œ ์•ž์œผ๋กœ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ ๊ฐ์ž ๋งŒ๋“ค ๋ถ€๋ถ„์„ ๋‚˜๋ˆ„์–ด ์ฝ”๋“œ๋ฅผ ์งค ๊ฑฐ๋ผ๊ณ  ๋ง์”€ํ•˜์…จ๋‹ค. ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ๋ฅผ ๋‚˜๋Š” ์ž˜ ์•Œ์ง€๋งŒ, ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์ฝ์—ˆ์„ ๋•Œ์—๋Š” ๋‚˜๋งŒํผ ๊ตฌ์กฐ๋ฅผ ์ž˜ ๋ชจ๋ฅผ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฉ๋Œ€ํ•œ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ตฌ์กฐ๋ณ„๋กœ ์ž˜ ๋‚˜๋ˆ ์„œ ์ €์žฅํ•˜๋ฉด ํ˜‘์—…์— ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™๋‹ค.
๋˜ํ•œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ์—๋„ ๋ช‡ ๋ฐฑ์ค„์งœ๋ฆฌ ๊ธด ์ฝ”๋“œ๋ฅผ ๋’ค์ ธ๊ฐ€๋ฉฐ ์ฐพ๋Š”๋ฐ ์• ๋ฅผ ๋จน๋Š” ์ผ์ด ๋œํ•  ๊ฒƒ ๊ฐ™์•„์„œ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™๋‹ค. ์•ž์œผ๋กœ๋Š” ์ด๋ ‡๊ฒŒ ํด๋”๋ฅผ ์ฒด๊ณ„์ ์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ๊ด€๋ฆฌํ•ด์•ผ๊ฒ ๋‹ค!



profile
Web FE ๊ฐœ๋ฐœ์ž ์ทจ์ค€์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€