TIL.231107 React์ˆ™๋ จ

์•ˆ์€์ง€ยท2023๋…„ 11์›” 7์ผ
1
post-custom-banner

โ–ถ๏ธstyled-components์‚ฌ์šฉ๋ฒ•

๐Ÿ“Œstyled-components๋ž€ ๋ฆฌ์•กํŠธ์—์„œ CSS-in-JS ๋ฐฉ์‹์œผ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊พธ๋ฐ€์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ํŒจํ‚ค์ง€
๐Ÿ“ŒํŒจํ‚ค์ง€๋ž€, React์—๋Š” ์—†๋Š” ๊ธฐ๋Šฅ์ด์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ์ถ”๊ฐ€๋กœ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์จ๋“œํŒŒํ‹ฐ ํ”„๋กœ๊ทธ๋žจ

sc์˜ ๊ธฐ๋ณธ์ ์ธ ์›๋ฆฌ๋Š” ๊พธ๋ฏธ๊ณ ์ž ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ sc์˜ ๋ฐฉ์‹๋Œ€๋กœ ๋จผ์ € ๋งŒ๋“ค๊ณ 
๊ทธ ์•ˆ์— ์Šคํƒ€์ผ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹

import React from "react";
import styled from "styled-components";

const StBox = styled.div`
//๋ฐฑํ‹ฑ ์•ˆ์˜ css์š”์†Œ ์ฝ”๋“œ ์ž‘์„ฑ
`;
const App = () => {
  return <StBox>๋ฐ•์Šค</StBox>;
};
export default App;

โ˜‘๏ธ CSS-in-JS

๐Ÿ“ŒCSS-in-JS๋ž€, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ CSS์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹

1.CSS-in-JS ๋ฐฉ์‹์˜ ๊ฐ•์ ์ด ๋ฐ”๋กœ ์Šคํƒ€์ผ ์ฝ”๋“œ๋ฅผ JS์ฝ”๋“œ ์ž‘์„ฑํ•˜๋“ฏ์ด ์Šคํƒ€์ผ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
2.props๋ฅผ ํ†ตํ•ด์„œ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›๊ณ , ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•ด์„œ ์กฐ๊ฑด๋ถ€ ์Šคํƒ€์ผ๋ง์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • styled-component๋ฅผ ์ƒ์„ฑ
const StBox = styled.div``;
  • ์ƒ์„ฑํ•œ styled-component๋ฅผ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ
const App = () => {
  return (
    <div>
      <StBox>๋นจ๊ฐ„ ๋ฐ•์Šค</StBox>
      <StBox>์ดˆ๋ก ๋ฐ•์Šค</StBox>
      <StBox>ํŒŒ๋ž‘ ๋ฐ•์Šค</StBox>
    </div>
  );
};
  • props๋กœ ์ž์‹์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ
const App = () => {
  return (
    <div>
      <StBox borderColor="red">๋นจ๊ฐ„ ๋ฐ•์Šค</StBox>
      <StBox borderColor="green">์ดˆ๋ก ๋ฐ•์Šค</StBox>
      <StBox borderColor="blue">ํŒŒ๋ž‘ ๋ฐ•์Šค</StBox>
    </div>
  );
};
  • props ๋ถ€๋ชจ์ปด ์ž์‹์ปด์œผ๋กœ ๋‚ด๋ ค์ฃผ๊ธฐ
const StBox = styled.div`
  border: 1px solid ${(props) => props.borderColor};
`;

ํ•˜๋ฃจ๋ฅผ ๋งˆ์น˜๋ฉฐ

javascript โ†’ JSX โ†’ styled-components ๊นŒ์ง€ ์—ฐ๋‹ฌ์•„ ์ •์‹ ์—†์ด ์ •๋ณด๊ฐ€ ๋“ค์–ด์™€์„œ์ธ์ง€
๋„ˆ๋ฌด ๋ณต์žกํ•˜๋‹ค๐Ÿ˜ญ ๋ณด์ถฉ์ด ์‚ด๊ธธ์ด์ง€๋งŒ....... 12์‹œ๊ฐ„์”ฉ ๊ฐ•์˜์— ๊ณผ์ œ์— til์ž‘์„ฑ์— ํ๋ฉ” ํ•˜๋ฃจ๊ฐ€ ์ด๋ ‡๊ฒŒ ์งง์•˜๋‚˜...? ๋‚ด ์ธ์ƒ์—์„œ ์ด๋ ‡๊ฒŒ ํ•˜๋ฃจ๋ฅผ ๊ฝ‰ ์ฑ„์›Œ์„œ ์จ๋ณด๋Š”๊ฑด ์ฒ˜์Œ์ธ๋“ฏ? ๋ญ”๊ฐ€ ๋ฟŒ๋“ฏํ•˜๋‹ค๐Ÿ˜‹ ํ•˜๋ฃจํ•˜๋ฃจ๊ฐ€ ์งง๊ณ  ๋ฒ„๊ฒ์ง€๋งŒ ์—ด์‹ฌํžˆ ๋ณต์Šต๋ณต์Šต!!!!!! ๊ผญ ๋˜๊ณ ๋ง๊ฑฐ๋‹ค ํ”„๋ก ํŠธ์—”๋“œ!!!!!!!!!!!!!!!!!!!!!!

post-custom-banner

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

comment-user-thumbnail
2023๋…„ 11์›” 8์ผ

ํ•  ์ˆ˜ ์žˆ๋‹ค! ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž!!!๐Ÿ˜Š

1๊ฐœ์˜ ๋‹ต๊ธ€