Component Driven Development

1. Component Driven Development๋ž€?

CDD๋ž€?

  • ๋ถ€ํ’ˆ ๋‹จ์œ„๋กœ UI ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€๋Š” ๊ฐœ๋ฐœ ์ง„ํ–‰ ๊ฐ€๋Šฅ

  • ์ปดํฌ๋„ŒํŠธ ๋‹จ์œ„๋กœ ๋งŒ๋“ค์–ด ํŽ˜์ด์ง€๋ฅผ ์กฐ๋ฆฝํ•˜๋Š” ๊ฐœ๋ฐœ ๋ฐฉ์‹์ธ ์ƒํ–ฅ์‹ ๊ฐœ๋ฐœ์— ๊ฐ€๊นŒ์›€

CSS in JS

  • CSS ๊ตฌ์กฐํ™”๋ฅผ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์‹œ๋„

    • SASS๋ผ๋Š” CSS๋ฅผ ํ™•์žฅํ•ด ์ฃผ๋Š” ์Šคํฌ๋ฆฝํŒ… ์–ธ์–ด๋กœ ์ „์ฒ˜๋ฆฌ๊ธฐ ํ™œ์šฉํ–ˆ์œผ๋‚˜, ์ปดํŒŒ์ผ ๋œ CSS์šฉ๋Ÿ‰์ด ์ปค์กŒ์Œ

  • SASS์˜ ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์˜ ์ง€ํ–ฅ์ ๋“ค์„ ๊ฐ€์ง„ ๋‹ค์–‘ํ•œ CSS ๋ฐฉ๋ฒ•๋ก ์ด ๋Œ€๋‘๋˜์—ˆ์Œ

    • ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ

    • ์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐํ™” (์œ ์ง€๋ณด์ˆ˜ ์šฉ์ด)

    • ์ฝ”๋“œ์˜ ํ™•์žฅ์„ฑ

    • ์ฝ”๋“œ์˜ ์˜ˆ์ธก์„ฑ (ํด๋ž˜์Šค ๋ช… ๋“ฑ์œผ๋กœ)

    • Styled-Component: ์ƒํƒœ๋ฅผ ๊ฐ€์ง„ ์ปดํฌ๋„ŒํŠธ๋“ค๋กœ๋ถ€ํ„ฐ UI๋ฅผ ์™„์ „ํžˆ ๋ถ„๋ฆฌํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•„์ฃผ ๋‹จ์ˆœํ•œ ํŒจํ„ด์„ ์ œ๊ณต

2.CDD ๊ฐœ๋ฐœ๋„๊ตฌ

Styled Components

CSS๋ฅผ ์ปดํฌ๋„ŒํŠธํ™” ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, React ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, CSS๋ฅผ Javascript์•ˆ์— ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ์Œ

Styled Components ์„ค์น˜ํ•˜๊ธฐ

// package.json์— ์ฝ”๋“œ ์ถ”๊ฐ€
{
"resolutions": {
  "styled-components": "^5"
  }
}

Styled Components๋ฅผ ์‚ฌ์šฉํ•  ํŒŒ์ผ๋กœ ๋ถˆ๋Ÿฌ์™€์ฃผ๊ธฐ

import styled from "styled-components"

์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

// ๋”ฐ์˜ดํ‘œ๊ฐ€ ์•„๋‹Œ ๋ฐฑ ํ‹ฑ(`)์„ ์‚ฌ์šฉ
import styled from "styled-components";

// Styled Components๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ณ 
const BlueButton = styled.button`
  background-color: blue;
  color: white;
`;

export default function App () {
  // React ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋“ฏ ์‚ฌ์šฉ
  return <BlueButton>Blue Button</BlueButton>;
}

์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌํ™œ์šฉํ•ด ์ƒˆ๋กœ์šด ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

import styled from "styled-components";

const BlueButton = styled.button`
  background-color: blue;
  color: white;
  `;

// ๋งŒ๋“ค์–ด์ง„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌํ™œ์šฉํ•ด ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ
const BigBlueButton = styled(BlueButton)`
  padding: 10px;
  margin-top: 10px;
`;

// ์žฌํ™œ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌํ™œ์šฉ
const BigRedButton = styled(BigBlueButton)`
  background-color: red;
`;

export default function App() {
return (
  <>
    <BlueButton>Blue Button</BlueButton>
    <br />
    <BigBlueButton>Big Blue Button</BigBlueButton>
    <br />
    <BigRedButton>Big Red Button</BigRedButton>
  </>  
  );
}

Props ํ™œ์šฉํ•˜๊ธฐ

import styled from "styled-components";
import GlobalStyle from "./GlobalStyle";

// ๋ฐ›์•„์˜จ prop์— ๋”ฐ๋ผ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง ๊ฐ€๋Šฅ
// ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•ด <Button> ์ปดํฌ๋„ŒํŠธ์— skyblue ๋ผ๋Š” props๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
// ์žˆ์œผ๋ฉด ๋ฐฐ๊ฒฝ์ƒ‰์œผ๋กœ skyblue๋ฅผ, ์—†์„ ๊ฒฝ์šฐ white๋ฅผ ์ง€์ •ํ•ด ์คŒ
const Button1 = styled.button`
  background: ${(props) => (props.skyblue ? "skyblue" : "white")};
`;

export default function App() {
return (
  <>
    <GlobalStyle />
    <Button1>Button1</Button1>
    <Button1 skyblue>Button1</Button1>
  </>
  );
}

Props ๊ฐ’์œผ๋กœ ๋ Œ๋”๋งํ•˜๊ธฐ

import styled from "styled-components";
import GlobalStyle from "./GlobalStyle";

// ๋ฐ›์•„์˜จ prop ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ด์šฉํ•ด ๋ Œ๋”๋ง 
const Button1 = styled.button`
    background: ${(props) => (props.color ? props.color : "white")};
`;

// ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ๋„ ํ™œ์šฉ ๊ฐ€๋Šฅ
const Button2 = styled.button`
    background: ${(props) => props.color || "white"};
`;

export default function App() {
return (
    <>
      <GlobalStyle />
      <Button1>Button1</Button1>
      <Button1 color="orange">Button1</Button1>
      <Button1 color="tomato">Button1</Button1>
      <br />
      <Button2>Button2</Button2>
      <Button2 color="pink">Button2</Button2>
      <Button2 color="turquoise">Button2</Button2>
    </>
  );
}

3. useRef

DOM reference๋ฅผ ์ž˜ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” useRef

React๋Š” ์˜ˆ์™ธ์ ์ธ ์ƒํ™ฉ์—์„œ useRef๋กœ DOM ๋…ธ๋“œ, ์—˜๋ฆฌ๋จผํŠธ, React ์—˜๋ฆฌ๋จผํŠธ๋กœ ์ฃผ์†Œ๊ฐ’ ์ฐธ์กฐ ๊ฐ€๋Šฅ

const ์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๊ทธ๋ฆ‡ = useRef(์ฐธ์กฐ์ž๋ฃŒํ˜•)
// ์ด์ œ ์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๊ทธ๋ฆ‡ ๋ณ€์ˆ˜์— ์–ด๋–ค ์ฃผ์†Œ๊ฐ’์ด๋“  ๋‹ด์„ ์ˆ˜ ์žˆ์Œ
return (
  <div>
    <input ref={์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๊ทธ๋ฆ‡} type="text" />
      {/* React์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ref๋ผ๋Š” ์†์„ฑ์— ์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๊ทธ๋ฆ‡์„ ๊ฐ’์œผ๋กœ ํ• ๋‹นํ•˜๋ฉด*/}
      {/* ์ฃผ์†Œ๊ฐ’์„_๋‹ด๋Š”_๊ทธ๋ฆ‡ ๋ณ€์ˆ˜์—๋Š” input DOM ์—˜๋ฆฌ๋จผํŠธ์˜ ์ฃผ์†Œ๊ฐ€ ๋‹ด๊น€ */}
      {/* ํ–ฅํ›„ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ input DOM ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Œ */}
  </div>
);

์•„๋ž˜์˜ ์ œํ•œ๋œ ์ƒํ™ฉ์—์„œ useRef ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‚˜, React์˜ ํŠน์ง•์ด์ž ์žฅ์ ์ธ ์„ ์–ธํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์›์น™๊ณผ ๋ฐฐ์น˜๋˜๊ธฐ์— ํ•ด๋‹น ์ƒํ™ฉ ์ œ์™ธํ•œ ๊ฒฝ์šฐ useRef ๋‚จ์šฉ์€ ๋ถ€์ ์ ˆ

function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () => {
    inputEl.current.focus();
  };
  return (
    <>
      <input ref={inputEl} type="text" />
      <button onClick = {onButtonClick}>Focus the input</button>
    </>;
  )
}

Last updated