โœ๐Ÿป [Code Camp_TIL] 7์ผ์ฐจ: props ํ™œ์šฉ, setState ์›๋ฆฌ

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

TIL

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

ํ™œ์„ฑํ™” ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ


isActive, props ํ™œ์šฉํ•˜๊ธฐ

๊ธฐ์กด์— input์ฐฝ 3๊ฐœ์™€ ๋“ฑ๋กํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์—ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ชจ๋“  input์ฐฝ์— ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๋ฉด ๋ฒ„ํŠผ์˜ ์ƒ‰๊น”์ด ๋ฐ”๋€Œ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด์•˜๋‹ค. ์ฐธ๊ณ ๋กœ ์†Œ์Šค์ฝ”๋“œ๋Š” container, presenter, styles ํŒŒ์ผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

//BoardWrite.container.js

import { useState } from "react";
import { useMutation } from "@apollo/client";
import BoardWriteUI from "./BoardWrite.presenter";

export default function BoardWrite(props) {
  const [isActive, setIsActive] = useState(false);
  //presenter ํŒŒ์ผ์— ๋„˜๊ฒจ์คŒ. props drilling.

  const [writer, setWriter] = useState("");
  const [title, setTitle] = useState("");
  const [contents, setContents] = useState("");
  
  function onChangeWriter(event) {
    setWriter(event.target.value);
    if (writer && title && contents) {
      setIsActive(true);
    }
  }

  function onChangeTitle(event) {
    setTitle(event.target.value);
    if (writer && title && contents) {
      setIsActive(true);
    }
  }

  function onChangeContents(event) {
    setContents(event.target.value);
    if (writer && title && contents) {
      setIsActive(true);
    }
  }
  
  return (
    <div>
      <BoardWriteUI
        onClickSubmit={onClickSubmit}
        onChangeWriter={onChangeWriter}
        onChangeTitle={onChangeTitle}
        onChangeContents={onChangeContents}
        isActive={isActive}
        // isActive๋ผ๋Š” state๋ฅผ isActive๋ผ๋Š” ํ‚ค ๊ฐ’์— ๋‹ด์•„์คŒ.
      />
    </div>
  );

//BoardWrite.presenter.js

import { Btn } from "./BoardWrite.styles";

export default function BoardWriteUI(props) {
  return (
    <div>
      <div>
        ์ž‘์„ฑ์ž: <RedInput type="text" 
    onChange={props.onChangeWriter} />
        ์ œ๋ชฉ: <input type="text" 
onChange={props.onChangeTitle} />
        ๋‚ด์šฉ: <input type="text" 
onChange={props.onChangeContents} />
        <Btn isActive={props.isActive} 
onClick={props.onClickSubmit}>
          ๋“ฑ๋กํ•˜๊ธฐ
        </Btn>
        ;
      </div>
    </div>
  );
}

//BoardWrite.styles.js

import styled from "@emotion/styled";

export const Btn = styled.button`
  background-color: ${(props) => {
    //ํ™”์‚ดํ‘œ ํ•จ์ˆ˜: BoardWrite.presenter.js์—์„œ props ๋ฐ›์•„์™€์คŒ.
    return props.isActive ? "yellow" : ""; 
    // ์ƒ‰์ƒ์—์„œ ๋น„์–ด์žˆ๋Š” ๊ฐ’ -> ํšŒ์ƒ‰
  }};
`;

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด์ค€ ๊ฒฐ๊ณผ ์ž‘์„ฑ์ž, ์ œ๋ชฉ, ๋‚ด์šฉ input ์ฐฝ ์•ˆ์„ ์ฐจ๋ก€๋Œ€๋กœ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌด์ž‘์œ„์˜ ์ˆœ์„œ๋กœ ์ฑ„์›Œ๋„ฃ์—ˆ์„ ๋•Œ์—๋Š” ๋ฒ„ํŠผ์˜ ์ƒ‰๊น”์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ๋ฌธ์ œ์ ์ด ์ƒ๊ฒผ๋‹ค.


setState ๋ฆฌ๋ Œ๋”


์›์ธ์„ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” setState์˜ ํŠน์ง•์„ ์•Œ์•„์•ผ ํ•œ๋‹ค.

setState๋Š” ๋น„๋™๊ธฐ๋กœ ์ž‘๋™ํ•œ๋‹ค. state๊ฐ’์„ ๋ฐ”๊พธ๊ณ  ๋ฐ”๋€ state ๊ฐ’์œผ๋กœ html์„ ๋‹ค์‹œ ๊ทธ๋ ค์ฃผ๋Š”๋ฐ(๋ฆฌ๋ Œ๋”๋ผ ํ•จ), ์ด๋•Œ ์ž„์‹œ ์ €์žฅ์†Œ๋ผ๋Š” ๊ณณ์— ์ฝ”๋“œ๋ฅผ ๋๊นŒ์ง€ ์ฝ์„ ๋•Œ๊นŒ์ง€ ๋ฐ”๋€ state ๊ฐ’์„ ์ €์žฅํ•ด๋‘์—ˆ๋‹ค๊ฐ€ ํ•œ๋ฒˆ์— ๋ฐ”๊ฟ”์„œ ์ถœ๋ ฅํ•œ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž.


function onClickCount() {
setCount(5) 
setCount(7)
setCount(9)
setCount(3) 
}

setState(setCount)๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๋งˆ๋‹ค ์ž„์‹œ์ €์žฅ๊ณต๊ฐ„์— state์ธ count๊ฐ€ 5๋กœ ์ €์žฅ๋˜๋„๋ก ํ•œ๋‹ค. ์ž„์‹œ์ €์žฅ๊ณต๊ฐ„์— 5, 7, 9, 3 ๋ˆ„์ ํ•ด๋†“๋‹ค๊ฐ€ 3์œผ๋กœ ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋ฉด 1๋ฒˆ๋งŒ ๋ฆฌ๋ Œ๋”ํ•œ๋‹ค. ๋งค๋ฒˆ ๋น„ํšจ์œจ์ ์œผ๋กœ ๋ฆฌ๋ Œ๋”ํ•˜์ง€ ์•Š๋Š”๋‹ค.


๋ฌธ์ œ๋Š”, ์ž„์‹œ์ €์žฅ์†Œ์— ์ €์žฅํ•ด๋‘๋Š” ๋™์•ˆ์—๋Š” ์ฝ”๋“œ๊ฐ€ ๋๋‚˜์ง€ ์•Š๋Š” ํ•œ ๋นˆ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค์‹œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ์ฝ”๋“œ๋กœ ๋Œ์•„๊ฐ€๋ณด์ž.

function onChangeWriter(event) {
    setWriter(event.target.value);
    if (writer && title && contents) {
      setIsActive(true);
    }
  }

  function onChangeTitle(event) {
    setTitle(event.target.value);
    if (writer && title && contents) {
      setIsActive(true);
    }
  }

  function onChangeContents(event) {
    setContents(event.target.value);
    if (writer && title && contents) {
      setIsActive(true);
    }
  }

setState(event.target.value)์—์„œ ์ž…๋ ฅํ•œ ๊ฐ’์ด ์ž„์‹œ์ €์žฅ๊ณต๊ฐ„์— ์ €์žฅ๋˜๊ณ , ์‹ค์ œ ๊ฐ’์ด ๋ฐ”๋€Œ์ง€๋Š” ์•Š์€ ์ƒํƒœ๋กœ if ๋ฌธ์—์„œ ๊ฒ€์ฆ์„ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ•จ์ˆ˜๊ฐ€ ์•„์ง ๋๋‚˜์ง€ ์•Š์•˜์„ ๋•Œ์—๋Š” setState๊ฐ€ ๋นˆ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ฆ ๊ณผ์ •์—์„œ ๋นˆ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์„œ ๊ฒ€์ฆ์„ ํ•˜๊ณ , ๊ฒฐ๊ณผ์ ์œผ๋กœ ์กฐ๊ฑด๋ฌธ์ด ์„ฑ๋ฆฝํ•˜์ง€ ์•Š๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.


๋”ฐ๋ผ์„œ input ์ฐฝ์— ๊ฐ’์ด ๋“ค์–ด์™”์„ ๋•Œ ์ž„์‹œ์ €์žฅ๊ณต๊ฐ„์— ๋“ค์–ด๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ if ๋ฌธ ๊ฒ€์ฆ ์‹œ ๋ฐ”๋กœ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋นˆ ๊ฐ’์œผ๋กœ ๊ฒ€์ฆ์„ ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค!

function onChangeWriter(event) {
    setWriter(event.target.value);
    if (event.target.value && title && contents) {
      setIsActive(true);
    }
  }

  function onChangeTitle(event) {
    setTitle(event.target.value);
    if (writer && event.target.value && contents) {
      setIsActive(true);
    }
  }

  function onChangeContents(event) {
    setContents(event.target.value);
    if (writer && title && event.target.value) {
      setIsActive(true);
    }
  }

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


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

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