[๐Ÿ”ซ][W-Guardian] Style-Component Conditional CSS

Hazยท2024๋…„ 6์›” 12์ผ

Trouble Shooting

๋ชฉ๋ก ๋ณด๊ธฐ
4/4

styled-component error message

Styled-Component ์กฐ๊ฑด๋ถ€ CSS ์„ค์ •ํ•˜๊ธฐ

๋ฐฉ๋ฒ•

const Container = styled.div<{ $bgcolor?: string }>`
  background-color: ${(props) =>
    props.$bgcolor === bgBright
      ? "#f6e8cf"
      : props.$bgcolor === bgCloudy
      ? "#dbe1da"
      : "#233947"};
`;

// API ๋ฐ์ดํ„ฐ ๋ฐ›์•„์™€ props๋กœ ์ „๋‹ฌ
<Container $bgcolor={weather[0].icon}>

์‚ผํ•ญ์—ฐ์‚ฐ์ž ๋‚ด๋ถ€์— ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์—ฐ๋‹ฌ์•„ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด ๊ฐ€๋…์„ฑ์„ ์ €ํ•˜ํ•  ์ˆ˜ ์žˆ์–ด์„œ ๊ทธ๋‹ค์ง€ ๋งŽ์ด ์“ฐ๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ์ง€๋งŒ, styled-component์—์„œ ์กฐ๊ฑด๋ถ€๋กœ CSS๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

๋ฐฐ๊ฒฝ์ƒ‰์„ ๋ฐ›์•„์˜จ API ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ๋ถ„๊ธฐ์ ์ด 2๊ฐœ์˜€๊ธฐ ๋•Œ๋ฌธ์— ํฌ๊ฒŒ ๊ฐ€๋…์„ฑ์„ ํ•ด์น˜์ง€ ์•Š๋Š” ์„ ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์ด์Šˆ ๋ฐœ์ƒ

1. Attribute ์ด๋ฆ„ ๋Œ€์†Œ๋ฌธ์ž ์ด์Šˆ

Warning: React does not recognize the bgColor prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase bgColor instead. If you accidentally passed it from a parent component, remove it from the DOM element.

Attribute์— bgColor๋กœ ์ด๋ฆ„์„ ์„ค์ •ํ•˜๊ณ  ๊ฐ’์„ ๋„˜๊ฒผ๋”๋‹ˆ React์—์„œ prop์œผ๋กœ ์ธ์‹ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. DOM์˜ Custom Attribute๋กœ ์˜๋„์ ์œผ๋กœ ๋งŒ๋“  ๊ฒฝ์šฐ๋ผ๋ฉด ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•˜๋ผ๊ณ  ์•Œ๋ ค์ค˜์„œ ์ด ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ–ˆ๋”๋‹ˆ ํ•ด๋‹น ์—๋Ÿฌ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜๊ณ  ์กฐ๊ฑด๋ถ€๋กœ ์„ค์ •ํ•œ CSS๋„ ์ž˜ ์ ์šฉ๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์˜€๋‹ค(ํ•˜์ง€๋งŒ ๊ทธ๊ฑด ์ฐฉ๊ฐ์ด์—ˆ๋‹ค).

2. Styled-Component unknown prop ์ด์Šˆ

styled-components: it looks like an unknown prop "bgcolor" is being sent through to the DOM, which will likely trigger a React console error.
If you would like automatic filtering of unknown props, you can opt-into that behavior via
(connect an API like @emotion/is-prop-valid )
or consider sing transient props ($prefix for automatic filtering.)

styled-component์—์„œ ์“ฐ๊ธฐ ์œ„ํ•ด ์œ„์—์„œ ๋งŒ๋“  custom attribute๊ฐ€ React ์ฝ˜์†”์—์„œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œ์ผฐ๋‹ค. ์ฆ‰, ์กฐ๊ฑด๋ถ€ CSS๊ฐ€ ์ž‘๋™์€ ํ•˜์ง€๋งŒ ์ž๋™์œผ๋กœ ํ•„ํ„ฐ๋ง์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค. ์นœ์ ˆํ•˜๊ฒŒ๋„ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ์ด์Šˆ ์ œ์ผ ๋์— ์จ์žˆ์—ˆ๊ณ , ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

  // ๊ธฐ์กด ์ฝ”๋“œ
  const Container = styled.div<{ $bgcolor?: string }>`
  background-color: ${(props) =>
    props.bgcolor === bgBright
      ? "#f6e8cf"
      : props.bgcolor === bgCloudy
      ? "#dbe1da"
      : "#233947"};
`;

  <Container bgcolor={weather[0].icon}>
  
 // ์ˆ˜์ • ์ฝ”๋“œ
 const Container = styled.div<{ $bgcolor?: string }>`
  background-color: ${(props) =>
    props.$bgcolor === bgBright
      ? "#f6e8cf"
      : props.$bgcolor === bgCloudy
      ? "#dbe1da"
      : "#233947"};
`;

<Container $bgcolor={weather[0].icon}>   
profile
๋‚˜๋„ ์žฌ๋ฐŒ๊ณ , ๋‚จ๋“ค๋„ ์žฌ๋ฐŒ๋Š” ์„œ๋น„์Šค ๋งŒ๋“ค์–ด๋ณด๊ณ  ์‹ถ๋‹ค๐Ÿ˜Ž

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