transitent props

transient props ์ผ์‹œ์ ์ธ props ์ด๋ผ๋Š” ๋œป์ด๋‹ค.

styled-components ์—์„œ ๋งŒ๋“  ๊ฐœ๋…์ธ๋ฐ, ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜๋ฉด "์กฐ๊ฑด๋ถ€ ์Šคํƒ€์ผ๋ง์ด ํ•„์š”ํ• ๋•Œ, ์ผ์‹œ์ ์œผ๋กœ ์“ฐ์ด๊ธฐ ์œ„ํ•œ Props" ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.

์™œ ์ผ์‹œ์ ์ธ Props๊ฐ€ ํ•„์š”ํ•œ์ง€ ์•„๋ž˜ ์˜ˆ์‹œ์™€ ํ•จ๊ป˜ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

React์—์„œ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌ๋˜๋Š” ๋ชจ๋“  Props๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ ์ตœ์ƒ์œ„ DOM ์š”์†Œ๋กœ ์ „๋‹ฌ๋œ๋‹ค. ์ด๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์ง€๋งŒ, ๋•Œ๋กœ๋Š” ์˜๋„ํ•˜์ง€ ์•Š์€ ๊ฒฐ๊ณผ๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

const StyledButton = styled.button`
  background-color: ${(props) => (props.isActive ? "blue" : "gray")};
`;

<StyledButton isActive={true}>Click me</StyledButton>;

์ด ๊ฒฝ์šฐ, isActive prop์€ ์Šคํƒ€์ผ๋ง์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜์ง€๋งŒ, React๋Š” ์ด prop์„ ์‹ค์ œ DOM ์š”์†Œ์ธ <button /> ์—๋„ ์ „๋‹ฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ isActive ๋Š” HTML button ์š”์†Œ์˜ ์œ ํšจํ•œ ์†์„ฑ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— React๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ๊ณ ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

Warning: Received `true` for a non-boolean attribute `isActive`.
If you want to write it to the DOM, pass a string instead: isActive="true" or isActive={value.toString()}.

์ด๋Ÿฌํ•œ ๊ฒฝ๊ณ ๋Š” ์ฝ”๋“œ๊ฐ€ ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, DOM์— ๋ถˆํ•„์š”ํ•œ ์†์„ฑ์ด ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

์•„๋ž˜๋Š” ํ•ด๊ฒฐ์„ ์œ„ํ•ด transient props๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ์ด๋‹ค.

const StyledButton = styled.button`
  background-color: ${(props) => (props.$isActive ? "blue" : "gray")};
`;

<StyledButton $isActive={true}>Click me</StyledButton>;

์ด๋ ‡๊ฒŒ transiten props๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ถˆํ•„์š”ํ•œ DOM ์†์„ฑ์„ ๋ฐฉ์ง€ํ•˜๊ณ , React๊ฐ€ ์•Œ ์ˆ˜ ์—†๋Š” props์— ๋Œ€ํ•ด ๊ฒฝ๊ณ ํ•˜๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ attribute์™€ ํ˜•ํƒœ๋ฅผ ๋‹ฌ๋ฆฌํ•จ์œผ๋กœ์„œ ์˜๋ฏธ์ ์œผ๋กœ๋„ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

Last updated