[React] Styled Components ์‚ฌ์šฉํ•˜๊ธฐ ๐Ÿ’…

์›์„ค์•„ยท2023๋…„ 8์›” 17์ผ
1

React

๋ชฉ๋ก ๋ณด๊ธฐ
9/11
post-thumbnail

๐Ÿ’… Styled Components๋ž€?

JS ์•ˆ์— CSS๊ฐ€ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ธ CSS in JS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜๋กœ, React์—์„œ CSS๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ํ•œ ๊ฐ€์ง€์ด๋‹ค.

์Šคํƒ€์ผ์— ๋Œ€ํ•œ ๊ณ ์œ  ํด๋ž˜์Šค๋ช…์„ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต์ด๋‚˜ ์˜คํƒ€์— ๋Œ€ํ•œ ๊ฑฑ์ •์ด ์—†๊ณ , Props์— ๋”ฐ๋ผ ์Šคํƒ€์ผ์ด ๋‹ฌ๋ผ์ง€๊ฒŒ๋” ๊ฐ€๋ณ€ ์Šคํƒ€์ผ๋ง์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

๐Ÿ‘จโ€๐Ÿ’ป Styled Components๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž!

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

$ npm i styled-components

์ปค๋งจ๋“œ ์ฐฝ์— ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅํ•ด ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด ์ค€๋‹ค.

import ํ•˜๊ธฐ

import styled from "styled-components";

์„ค์น˜๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด Styled Components๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด styled-components ํŒจํ‚ค์ง€์—์„œ styled ํ•จ์ˆ˜๋ฅผ importํ•œ๋‹ค.

๊ธฐ๋ณธ ๋ฌธ๋ฒ•

Styled Components๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ html์˜ ๋ชจ๋“  ํƒœ๊ทธ๋“ค์— ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

import styled from "styled-components";

const ๋ณ€์ˆ˜์ด๋ฆ„ = styled.HTML์—˜๋ฆฌ๋จผํŠธ`
	// CSS ์Šคํƒ€์ผ
`;

๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ const ๋ณ€์ˆ˜์ด๋ฆ„ = ๋‹ค์Œ์— styled.div, styled.button๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๊ณ , ๋ฐฑํ‹ฑ ํƒœ๊ทธ(``) ์•ˆ์— CSS ์Šคํƒ€์ผ์„ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

import styled from "styled-components";

const StyledSection = styled.div`
	background-color: red;
	display: flex;
	justify-content: center;
`;

export default function Section() {
 return <StyledSection></StyledSection>; 
}

์˜ˆ๋ฅผ ๋“ค์–ด, ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!
์ด์ฒ˜๋Ÿผ ์Šคํƒ€์ผ์ด ์ ์šฉ๋œ StyledSection ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿค ๋ถ€๋ชจ ์š”์†Œ ์ƒ์†๋ฐ›์•„ ์Šคํƒ€์ผ๋งํ•˜๊ธฐ

const Title = styled.h1`
  color: #aac9f0;
`;

์œ„์™€ ๊ฐ™์€ Title ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ, Title์˜ ์Šคํƒ€์ผ์„ ์ƒ์†๋ฐ›์€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•ด ๋ณด์ž,

const SubTitle = styled(Title)`
	font-size: 12px;
    align-self: center;
`;

styled(๋ถ€๋ชจ ๋ณ€์ˆ˜)์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๋ถ€๋ชจ ๋ณ€์ˆ˜์˜ ์Šคํƒ€์ผ์„ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค!

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

const Section = styled.div`
  background-color: beige;
  border-radius: 15px;
`;

const Title = styled.h1`
  color: #aac9f0;
`;

const SubTitle = styled(Title)`
  font-size: 12px;
  align-self: center;
`;

export default function Content() {
  return (
    <Section>
      <Title>Section</Title>
      <SubTitle>I am a SubTitle</SubTitle>
    </Section>
  );
}

(์œ„ ์ฝ”๋“œ๊ฐ€ ์ ์šฉ์ด ๋œ ๋ชจ์Šต)


๐ŸŽจ Props์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š” ์Šคํƒ€์ผ๋ง

Styled Components๋กœ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌ๋ฐ›์€ Props์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฒ„ํŠผ์˜ ์Šคํƒ€์ผ์ด Props์— ๋”ฐ๋ผ ๋ฐ”๋€Œ๋„๋ก ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

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

const StyledButton = styled.div`
  background-color: ${({ primary }) => (primary ? "red" : "blue")};
  display: flex;
  justify-content: center;
  height: 20px;
  padding: 5px;
  margin: 5px;
  border-radius: 15px;
`;

export default function Button({ primary, text }) {
  return <StyledButton primary={primary}>{text}</StyledButton>;
}

<Button /> ์ปดํฌ๋„ŒํŠธ์— ๋„˜์–ด์˜จ primary์™€ text Props๋ฅผ <StyledButton />๋กœ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•œ๋‹ค. (<StyledButton />์ด Props๋ฅผ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค)


(<Button /> ์ปดํฌ๋„ŒํŠธ๋ฅผ <Content />์— ๋„ฃ์€ ๋ชจ์Šต, ๋นจ๊ฐ„ ์ƒ‰ ๋ฒ„ํŠผ์€ Primary Props๋ฅผ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฒ„ํŠผ)


์ถœ์ฒ˜ : https://velog.io/@sorious77/React-Styled-Components-์‚ฌ์šฉ-๋ฐฉ๋ฒ•-e9o4rnfq, https://www.daleseo.com/react-styled-components/, https://nykim.work/107

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

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

ํผ๊ฐ€์š”.. ^^

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