styled components attrs - boostcamp-2020/Project15-B-Client-Based-Formula-Editor GitHub Wiki

styled-components attrs

styled-components์˜ attrs ์†์„ฑ์€ ์ •์ ์ธ ์†์„ฑ, ์˜ˆ๋ฅผ๋“ค๋ฉด input์˜ type์ด๋‚˜ radio ๋ฒ„ํŠผ์˜ checked์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋™์ ์œผ๋กœ ๋ณ€ํ™”ํ•˜๋Š” ์†์„ฑ์— ๋Œ€ํ•ด์„œ๋„ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ ํ™” ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ๊ฒช์—ˆ๋˜ ์ด์Šˆ๋Š” attrs๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ๋‚ด๋ถ€์— "${}"๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ onChange์— ๋”ฐ๋ผ ์ƒ‰๋ณ€๊ฒฝ์„ ํ•ด์ฃผ์—ˆ์—ˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ๋˜๋‹ˆ ๋ ‰์ด ๊ฑธ๋ฆฌ๊ณ  Over 200 classes were generated for component styled.div. Consider using the "attrs" method, together with a style object for frequently changed styles. ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ƒํ™ฉ์—์„œ attrs๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ตœ์ ํ™”๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

// ๊ธฐ์กด ์ฝ”๋“œ
const Layout = styled.div`
  font-size: 16px;
  color: ${props => props.color};
`;

// attrs๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ
const Layout = styled.div.attrs(props => ({
  color: props.color,
}))`
  font-size: 16px;
`;