๐ styled-components๋ ์ฐ๋ฆฌ๊ฐ ๋ฆฌ์กํธ์์ CSS-in-JS ๋ฐฉ์์ผ๋ก ์ปดํฌ๋ํธ๋ฅผ ๊พธ๋ฐ์ ์๊ฒ ๋์์ฃผ๋ ํจํค์ง!
(1) VSCode Extension ์ค์น
์ด ์ต์คํ
์
์ ์ค์นํ๋ฉด styled-components์์์ ์คํ์ผ ์ฝ๋๋ฅผ ํธํ๊ฒ ์์ฑํ ์ ์๋ค!
(2) styled-components ์ค์น
npm install styled-components
๋๋
yarn add styled-components
styled components์ ๊ธฐ๋ณธ์ ์ธ ์๋ฆฌ๋ ๊พธ๋ฏธ๊ณ ์ ํ๋ ์ปดํฌ๋ํธ๋ฅผ styled components์ ๋ฐฉ์๋๋ก ๋จผ์ ๋ง๋ค๊ณ , ๊ทธ ์์ ์คํ์ผ ์ฝ๋๋ฅผ ์์ฑํ๋ ๋ฐฉ์์ด๋ค.
// App.js
import React from "react";
// styled-components์์ styled ๋ผ๋ ํค์๋๋ฅผ import ํฉ๋๋ค.
import styled from "styled-components";
// styledํค์๋๋ฅผ ์ฌ์ฉํด์ styled-components ๋ฐฉ์๋๋ก ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ญ๋๋ค.
const StBox = styled.div`
width: 100px;
height: 100px;
border: 1px solid red;
margin: 20px;
`; // ์ด ์์ ์คํ์ผ ์ฝ๋๋ฅผ ์์ฑํ๋ค. ์คํ์ผ ์ฝ๋๋ ์ฐ๋ฆฌ๊ฐ ์๊ณ ์๋ css์ ๋์ผํ๋ค.
const App = () => {
// ์ฐ๋ฆฌ๊ฐ ๋ง๋ styled-components๋ฅผ JSX์์ html ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋ฏ์ด ์ฌ์ฉํ๋ค.
return <StBox>๋ฐ์ค</StBox>;
};
export default App;
ํต์ฌ : const StBox = styled.div``;
styled ๋ค์๋ html ํ๊ทธ๊ฐ ์จ๋ค.
ex) styled.span
, styled.button
styled-components๋ ์ด๋ฆ ๊ทธ๋๋ก ์ปดํฌ๋ํธ์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์ฐ๋ฆฌ๋ ๊ฐ ์ปดํฌ๋ํธ์๊ฒ props๋ฅผ ํตํด borderColor์ ๋ํ ์ ๋ณด๋ฅผ ์ ๋ฌํด์ค ์ ์๋ค.
import "./App.css";
import styled from "styled-components";
const StContainer = styled.div`
display: flex;
`;
const StBox = styled.div`
width: 100px;
height: 100px;
border: 1px solid ${(props) => props.$borderColor}; โญ๏ธ
margin: 20px;
`;
function App() {
return (
<StContainer>
<StBox $borderColor="red">์๋
</StBox>
<StBox $borderColor="blue">์๋
</StBox>
<StBox $borderColor="green">์๋
</StBox>
</StContainer>
);
}
export default App;
[๋์ํ๋ ๊ณผ์ ]
1. ๋จผ์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด${ }
๋ฅผ ์ด์ด์ค๋๋ค.
2. ๋น์ด์๋ ํ์ดํ ํจ์๋ฅผ ์ด์ด ์ค๋๋ค.${( )=>{ }}
์ด๋ ๊ฒ!
3. ํจ์์ ์ธ์์์ props๋ฅผ ๋ฐ์์ค๊ณ , props์์๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ๋ณด๋ธ borderColor๊ฐ ์๋ค. ๊ทธ๊ฒ์ return ํ๋ค.
4.${(props)โ{ return props.$borderColor }}
์ด๋ ๊ฒ ๋ฆฌํดํ๋ฉด(props)โ{ return props.$borderColor }
์ ๊ฐ์ด === 'red' ๊ฐ ๋๊ณ ๊ฒฐ๊ตญ ์ฐ๋ฆฌ ๋์๋ ๋ณต์กํด๋ณด์ด์ง๋ง, ๋ธ๋ผ์ฐ์ ๋border: 1px solid red
๋ผ๋ ์ฝ๋๋ก ์ธ์ํ์ฌ ์คํ์ผ๋ง์ด ๋๋ ๊ฒ ์ด๋ค.
import "./App.css";
import styled from "styled-components";
const StContainer = styled.div`
display: flex;
`;
const StBox = styled.div`
width: 100px;
height: 100px;
border: 1px solid ${(props) => props.$borderColor};
margin: 20px;
`;
const boxList = ["red", "blue", "green", "black"];
const getBoxName = (color) => {
switch (color) {
case "red":
return "๋นจ๊ฐ ๋ฐ์ค";
case "blue":
return "ํ๋ ๋ฐ์ค";
case "green":
return "์ด๋ก ๋ฐ์ค";
default:
return "๊ฒ์ ๋ฐ์ค";
}
};
function App() {
return (
<StContainer>
{boxList.map((box, idx) => (
<StBox key={idx} $borderColor={box}>
{getBoxName(box)}
</StBox>
))}
</StContainer>
);
}
export default App;
์ฐ๋ฆฌ๋ ํ๋ก์ ํธ์ ๊ณตํต์ ์ผ๋ก ์ ์ฉํด์ผ ํ ์คํ์ผ์ด ์์ ์ ์๋ค.
๊ทธ๋ด ๋ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ด ๋ฐ๋ก ์ ์ญ ์คํ์ผ๋ง ์ด๋ค.
// TestPage.jsx
import styled from "styled-components";
function TestPage(props) {
return (
<Wrapper>
<Title>{props.title}</Title>
<Contents>{props.contents}</Contents>
</Wrapper>
);
}
const Title = styled.h1`
font-family: "Helvetica", "Arial", sans-serif;
line-height: 1.5;
font-size: 1.5rem;
margin: 0;
margin-bottom: 8px;
`;
const Contents = styled.p`
margin: 0;
font-family: "Helvetica", "Arial", sans-serif;
line-height: 1.5;
font-size: 1rem;
`;
const Wrapper = styled.div`
border: 1px solid black;
border-radius: 8px;
padding: 20px;
margin: 16px auto;
max-width: 400px;
`;
export default TestPage;
๋ชจ๋ ์ปดํฌ๋ํธ์ font-family
์ line-height
์์ฑ์ด ๊ณตํต์ ์ผ๋ก ๋ค์ด์๋ค.
์ด๋ ๊ณตํต์ ์ผ๋ก ์ ์ฉ๋๋ ๋ถ๋ถ์ ๋นผ์ global style๋ก ์ ์ฉํ ํ์๊ฐ ์๋ค.
// GlobalStyle.jsx
import { createGlobalStyle } from "styled-components";
const GlobalStyle = createGlobalStyle`
body {
font-family: "Helvetica", "Arial", sans-serif;
line-height: 1.5;
}
`;
export default GlobalStyle;
// App.jsx
import GlobalStyle from "./GlobalStyle";
import BlogPost from "./BlogPost";
function App() {
const title = '์ ์ญ ์คํ์ผ๋ง ์ ๋ชฉ์
๋๋ค.';
const contents = '์ ์ญ ์คํ์ผ๋ง ๋ด์ฉ์
๋๋ค.';
return (
<>
<GlobalStyle /> // ์ต์์
<BlogPost title={title} contents={contents} />
</>
);
}
export default App;
๊ธ๋ก๋ฒ ์คํ์ผ ์ปดํฌ๋ํธ๋ฅผ ๋ฃจํธ ์ต์์์ ๋ฃ์ด์ฃผ๋ฉด ํ์์ ์ปดํฌ๋ํธ๋ค์๊ฒ ๊ธ๋ก๋ฒ ์คํ์ผ์ด ์ ์ฉ๋๋ค.
๋๋์
์ด๋ ค์ด ๊ฒ์ด ์๋๋ผ ์ต์ํ์ง ์์ ๊ฒ์ด๋ค!๐ค
๊ณ์ ์ฌ์ฉํด๋ณด๋ฉด์ ์ต์ํด์ง ํ์๊ฐ ์๋ค.