React ์ปดํฌ๋ํธ css ์คํ์ผ๋ง ๊ธฐ๋ณธ / Style Components(prop๋ก ํ์ฅ, as, ์์ฑ ์ถ๊ฐ, ์ ๋๋ฉ์ด์ , ๊ฐ์ ์ ํ์)
React ์ปดํฌ๋ํธ css ์คํ์ผ๋ง ๊ธฐ๋ณธ
๊ธฐ๋ณธ์ด๋ผ๊ธฐ ๋ณด๋ค๋, react์ปดํฌ๋ํธ๋ฅผ ๋ณ๋์ css-in-js๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ด ์์ํ๊ฒ css๋ก๋ง ์คํ์ผํ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ๋ค.
Inline Style
๊ฐ์ฅ ๊ฐ๋จํ๊ณ ์ฌ์ด ๋ฐฉ๋ฒ์ React ์ปดํฌ๋ํธ์ css์ธ๋ผ์ธ ์คํ์ผ์ ๋ฐ๋ก ์ ์ฉํ๋ ๊ฒ์ด๋ค.
์ผ๋ฐ์ ์ธ ์น ํ์ด์ง์์ ์ธ๋ผ์ธ ์คํ์ผ์ ์ ์ฉํ ๋์ฒ๋ผ html์๋ฆฌ๋จผํธ์ style์์ฑ์ ์ด์ฉํ๋ฉด ๋๋ค.
ํ์ง๋ง, React๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์์ฑํ๊ธฐ ๋๋ฌธ์ ์นํ์ด์ง์์ ์ธ๋ผ์ธ ์คํ์ผ์ ์ ์ฉํ ๋์ ์ฝ๊ฐ์ ์ฐจ์ด์ ์ด ์๋ค.
-style ์์ฑ๊ฐ์ ์ผ๋ฐ ๋ฌธ์์ด์ด ์๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๊ฐ ํ ๋น๋์ผ ํ๋ค.
- CSS ์์ฑ๋ช ์ด ์ผ๋ฐฅ ์ผ์ด์ค(kebab case)์ด ์๋ ์นด๋ฉ ์ผ์ด์ค(camel case)๋ก ์์ฑ๋์ผ ํ๋ค.
์ด๋ ์นด๋ฉ ์ผ์ด์ค๋ var camelCase; ์ ๊ฐ์ด ์ค๊ฐ ๊ธ์๋ค์ ๋๋ฌธ์๋ก ์์ํ์ง๋ง ์ฒซ ๊ธ์๊ฐ ์๋ฌธ์์ธ ๊ฒฝ์ฐ์๋ ๋ํ์ ๋ชจ์์ด ๋น์ทํ๋คํ์ฌ ์นด๋ฉ ์ผ์ด์ค๋ผ๊ณ ํ๋ค.
import React from "react";
const btnStyle = {
color: "white",
background: "teal",
padding: ".375rem .75rem",
border: "1px solid teal",
borderRadius: ".25rem",
fontSize: "1rem",
lineHeight: 1.5,
};
function Button() {
return <button style={btnStyle}>Inline</button>;
}
External Stylesheet
React์ปดํฌ๋ํธ ํ์ผ์์ ํด๋น cssํ์ผ์ ์ํฌํธ ํ๋ ๋ฐฉ๋ฒ
๊ทธ ๋ค์, ์๋ฆฌ๋จผํธ์ className์์ฑ์ ์ด์ฉํด์ ์ธ๋ถ ํ์ผ์ ์ ์๋ ์คํ์ผ์ ๋งตํ์์ผ์ฃผ๋ ๊ฒ
์๋ฅผ ๋ค์ด, Button.cssํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ์คํ์ผ์ ์ ์ํ๋ค.
.btn {
color: white;
background: teal;
padding: 0.375rem 0.75rem;
border: 1px solid teal;
border-radius: 0.25rem;
font-size: 1rem;
line-height: 1.5;
}
๊ทธ๋ฆฌ๊ณ ๋ฒํผ ์ปดํฌ๋ํธ๊ฐ ์์ฑ๋ Button.jsํ์ผ์์ ๋ค์๊ณผ ๊ฐ์ด ์ cssํ์ผ์ ์ํฌํธํ๋ค.
์ด๋ ์์ฑ๋ช ์ class ๋์ ์ className๋ฅผ ์ฌ์ฉํด์ผํ๋ค.
import React from "react";
import "./Button.css";
function Button() {
return <button className="btn">External</button>;
}
์ด๋ฐ ๋ฐฉ์
CSS Modules
์์์ ์๊ฐํ ์ธ๋ถ ์คํ์ผ ์ํธ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ React์ฑ์ ๊ท๋ชจ๊ฐ ์ปค์ง์ ๋ฐ๋ผ css ํด๋์ค ์ด๋ฆ์ด ๊ฒน์น๊ฒ ๋ ๊ฐ๋ฅ์ฑ์ด ์ปค์ง๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๊ธ๋ก๋ฒ ๋ค์ ์คํ์ด์ค (global namespace)๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, ๋ง์ฝ 2๊ฐ์ cssํ์ผ์ ๋์ผํ ํด๋์ค์ ๋ํ ์ ์๊ฐ ๋์ด ์๋ค๋ฉด, ํด๋น ํด๋์ค๊ฐ ์ ์ฉ๋ ์๋ฆฌ๋จผํธ๋ 2๊ฐ์ ์คํ์ผ์ ๋ชจ๋ ์ํฅ์ ๋ฐ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ css ํ์ผ์ ๊ณ ์ ์ ๋ค์ ์คํ์ด์ค๋ฅผ ๋ถ์ฌํด์ฃผ๋ css๋ชจ๋(css modules)์ด๋ผ๋ ๊ธฐ๋ฒ์ด ์๋ค.
react์ปดํฌ๋ํธ์ css๋ชจ๋์ ํตํด ์คํ์ผ์ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค.
- ์ธ๋ถ ์คํ์ผ ์ํธ๋ฅผ ์์ฑํ ๋, .cssํ์ฅ์๊ธฐ ์๋, .module.cssํ์ฅ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
- react์ปดํฌ๋ํธ ํ์ผ์์ ์ํฌํธ ํ ๋, ์ํฌํธ๋ css๋ชจ๋์ ์ด๋ฆ์ ๋ช ์์ ์ผ๋ก ์ง์ ํด์ค๋ค. (import module_name from "./my/style.module.css";) ์ด๋ฐ ๋ฐฉ์
- ์๋ฆฌ๋จผํธ className ์์ฑ์ ํ ๋นํด์ค ๋, ํด๋น ํด๋์ค๊ฐ ์ด๋css ์์์ธ์ง ์๋ ค์ค๋ค.
import React from "react";
import styles from "./Button.module.css";
function Button() {
return <button className={styles.btn}>Module</button>;
}
export default Button;
์๋ฐ ๋ฐฉ์
Styled Components๋ก React ์ปดํฌ๋ํธ ์คํ์ผ
CSS in JS?
CSS in JS๋ ์คํ์ผ ์ ์๋ฅผ CSSํ์ผ์ด ์๋ JS๋ก ์์ฑ๋ ์ปดํฌ๋ํธ์ ๋ฐ๋ก ์ฝ์ ํ๋ ์คํ์ผ ๊ธฐ๋ฒ์ด๋ค.
์นํ์ด์ง๋ฅผ HTML, CSS, JS 3๊ฐ๋ก ๋ถ๋ฆฌํ๋ ๊ฒ์ด ๊ธฐ์กด์ ์น์ฌ์ดํธ๋ฅผ ๊ฐ๋ฐํ ๋ ๊ธฐ๋ณธ์ผ๋ก ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. ํ์ง๋ง React, Vue, Angular์ ๊ฐ์ ๋ชจ๋ js๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ธ๊ธฐ๋ฅผ ๋๋ฉด์ ์น ๊ฐ๋ฐ์ ํจ๋ฌ๋ค์์ด ๋ฐ๋๊ณ ์๋ค. ์ต๊ทผ์๋ ์ฌ๋ฌ ๊ฐ์ ์ฌํ์ฉ์ด ๊ฐ๋ฅํ ๋น๋ฉ ๋ธ๋ก์ผ๋ก ๋ถ๋ฆฌํ์ฌ ๊ฐ๋ฐํ๋ ์ปดํฌ๋ํธ ๊ธฐ๋ฐ ๊ฐ๋ฐ ๋ฐฉ๋ฒ์ด ์ฃผ๋ฅ๊ฐ ๋๊ณ ์๋ค.
๋ฐ๋ผ์, ๊ฐ ์ปดํฌ๋ํธ์ html, css, js๋ฅผ ๋ชฝ๋ ๋๋ ค๋ฐ๋ ํจํด์ด ๋ง์ด ์ฌ์ฉ๋๋ค. React๋ jsx๋ฅผ ์ฌ์ฉํด์ ์ด๋ฏธ js๊ฐ html์ ํฌํจํ๊ณ ์๋ ํํ๋ฅผ ์ทจํ๊ณ ์๋๋ฐ, ์ฌ๊ธฐ์ css-in-js๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ง ์ฌ์ฉํ๋ฉด css๋ ์ ์ฝ๊ฒ js์ ์ฝ์ ํ ์ ์๋ค.
ํจํค์ง ์ค์น
Styled Components๋ styled-components๋ผ๋ npmํจํค์ง ๋ช ์ ๊ฐ์ง๊ณ ์๋ค.
npm i styled-components
๊ธฐ๋ณธ ๋ฌธ๋ฒ
- html ํ๊ทธ์ ๋ํด์ ์ด๋ฏธ ์์ฑ์ด ์ ์๋์ด ์๊ธฐ ๋๋ฌธ์ ํด๋น ํ๊ทธ๋ช ์ ์์ฑ์ ์ ๊ทผํ๋ค.
import styled from "styled-components";
styled.button`
// <button> HTML ์๋ฆฌ๋จผํธ์ ๋ํ ์คํ์ผ ์ ์
`;
- React ์ปดํฌ๋ํธ๋ฅผ ์คํ์ผ๋ง ํ ๋๋ ํด๋น ์ปดํฌ๋ํธ๋ฅผ ์ํฌํธ ํ ํด๋น ์ธ์๋ก ๋๊ธฐ๋ฉด ๋๋ค.
import styled from "styled-components";
import Button from "./Button";
styled(Button)`
// <Button> React ์ปดํฌ๋ํธ์ ์คํ์ผ ์ ์
`;
๊ณ ์ ์คํ์ผ๋ง
import React from "react";
import styled from "styled-components";
const StyledButton = styled.button`
padding: 6px 12px;
border-radius: 8px;
font-size: 1rem;
line-height: 1.5;
border: 1px solid lightgray;
color: gray;
background: white;
`;
function Button({ children }) {
return <StyledButton>{children}</StyledButton>;
}
์ด๋ฐ ๋ฐฉ์์ผ๋ก ์ฌ์ฉ์ด ๋๋ค.
import Button from "./Button";
<Button>Default Button</Button>;
์ด๋ฐ ๋ฐฉ์์ผ๋ก ์คํ์ผ์ด ์ ์ฉ๋ ์ด ๋ฒํผ ์ปดํฌ๋ํธ๋ฅผ ๋ค๋ฅธ React์ปดํฌ๋ํธ์์ ์ฌ์ฉํ๋ค๋ฉด
<button class="sc-kgAjT beQCgz">Default Button</button>
๋ธ๋ผ์ฐ์ ์์ ๊ฐ๋ฐ์๋๊ตฌ๋ฅผ ๋๋ฅธ ํ, ์์ค๋ฅผ ๋ด๋ณด๋ฉด <button>html์๋ฆฌ๋จผํธ์ Styled Components๊ฐ ์๋์ผ๋ก ์์ฑํด์ค ํด๋์ค ์ด๋ฆ์ด ์ ์ฉ๋์ด ์์์ ์ ์ ์๋ค.
๊ฐ๋ณ ์คํ์ผ๋ง( porps ํ ๊ฐ ์ ๋ฌ )
Styled Compnents๋ React ์ปดํฌ๋ํธ์ ๋์ด์จ porps์ ๋ฐ๋ผ ๋ค๋ฅธ ์คํ์ผ์ ์ ์ฉํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
Tagged Template Literals์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ํจ์๋ ๋ฌธ์์ด ์์ ํฌํจ์ํฌ ์ ์๋ค.
import React, { useState } from "react";
import styled from "styled-components";
function Example() {
const [email, setEmail] = useState("");
return (
<ExampleWrap active={email.length}>
<Button>Hello</Button>
<NewButton color="blue">Im new Button</NewButton>
</ExampleWrap>
);
}
const ExampleWrap = styled.div`
background: ${({ active }) => {
if (active) {
return "white";
}
return "#eee";
}};
color: black;
`;
const Button = styled.button`
width: 200px;
padding: 30px;
`;
// Button ์ปดํฌ๋ํธ ์์
const NewButton = styled.Button`
// NewButton ์ปดํฌ๋ํธ์ color๊ฐ๋ props๊ฐ ์์ผ๋ฉด ๊ทธ ๊ฐ ์ฌ์ฉ, ์์ผ๋ฉด 'red' ์ฌ์ฉ
color: ${props => props.color || "red"};
`;
export default Example;
- styled-component๋ฅผ ์ฌ์ฉํ๋ ์ฅ์ ์ค ํ๋๊ฐ ๋ณ์์ ์ํด ์คํ์ผ์ ๋ฐ๊ฟ ์ ์๋ค๋ ์ ์ด๋ค.
- ์ ์์๋ฅผ ๋ณด๋ฉด, email์ด๋ผ๋ state๊ฐ์ ๋ฐ๋ผ ExampleWrap์ prop์ผ๋ก ๋ด๋ ค์ค active๋ผ๋ ๊ฐ์ด true or false๋ก ๋ฐ๋๊ฒ ๋๋ค.
- styled-component๋ ๋ด๋ถ์ ์ผ๋ก props์ ๋ฐ์ ์ ์๊ณ , ๊ทธ props์ ๋ฐ๋ผ ์คํ์ผ์ ๋ณ๊ฒฝํ ์ ์๋ค.
- ํด๋น ํ์ผ์์ ์ ์ํ css ๋ฅผ exportํ์ฌ ๋ค๋ฅธ ํ์ผ์์ importํ ์ ์๋ค.
๊ฐ๋ณ ์คํ์ผ๋ง ( props ์ฌ๋ฌ ๊ฐ ์ ๋ฌ )
- prop ์ ๋ฐ๋ผ ๋ฐ๊พธ๊ณ ์ถ์ css์์ฑ์ด ์์ ๊ฐ์ด ํ๋๊ฐ ์๋๋ผ ์ฌ๋ฌ ๊ฐ์ผ ๊ฒฝ์ฐ๊ฐ ์๋ค.
- ์ด๋ด ๊ฒฝ์ฐ, Styled Components ์์ ์ ๊ณตํ๋ cssํจ์๋ฅผ ์ฌ์ฉํด์ ์ฌ๋ฌ ๊ฐ์ css์์ฑ์ ๋ฌถ์ด์ ์ ์ํ ์ ์๋ค.
- ์๋ฅผ ๋ค์ด, primary porp์ด ๋์ด์จ ๊ฒฝ์ฐ, ๊ธ์์์ ํฐ์, ๋ฐฐ๊ฒฝ์๊ณผ ๊ฒฝ๊ณ์์ ๋จ์์ผ๋ก ๋ณ๊ฒฝํ๊ณ ์ถ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ ์ฝ๋๋ฅผ ์์ ํ ์ ์๋ค. && ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ primary prop์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ์๋ง css๋ก ์ ์๋ ์คํ์ผ์ด ์ ์ฉ๋๋๋ก ํ์๋ค.
import React from "react";
import styled, { css } from "styled-components";
const StyledButton = styled.button`
padding: 6px 12px;
border-radius: 8px;
font-size: 1rem;
line-height: 1.5;
border: 1px solid lightgray;
${(props) =>
props.primary &&
css`
color: white;
background: navy;
border-color: navy;
`}
`;
function Button({ children, ...props }) {
return <StyledButton {...props}>{children}</StyledButton>;
}
- ${(props) => ...} ๋ถ๋ถ์ props.primary๊ฐ true์ธ ๊ฒฝ์ฐ์ css ์ฝ๋๋ฅผ ์ ์ฉ์ํจ๋ค.
- Button ์ปดํฌ๋ํธ๋ StyledButton ์ ๋ฐํํ๋ฉฐ, ๋ฒํผ ๋ด์ฉ children๊ณผ ๋ค๋ฅธ ์์ฑ props์ styledButton์ ์ ๋ฌํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด Button์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฒํผ์ ์์ฑํ๊ณ ์ํ๋ ์์ฑ์ ์ ๋ฌํ ์ ์๋ค.
- ์ฐธ๊ณ ๋ก ๋๊ฒจ์ผํ prop ๊ฐ์ด ๋ง์์ง ๊ฒฝ์ฐ, ์์ ๊ฐ์ด ...props๊ตฌ๋ฌธ์ ์ฌ์ฉํด์ children์ธ์ ๋ชจ๋ prop์ ๊ฐํธํ๊ฒ ์ ๋ฌํ ์ ์๋ค.
์์ ๋ ๋ณด๊ธฐ & ์์ฑ ๋ ๋ณด๊ธฐ
... ์๋ต
const Box = styled.div`
background-color: ${(props) => props.bgColor};
width: 100px;
height: 100px;
`;
const App = () => {
return (
<Wrapper>
<Box bgColor={"#cf6a87"} />
<Box bgColor={"#574b90"} />
</Wrapper>
);
};
- BoxOne, BoxTwo ๋๊ฐ์ง ์ปดํฌ๋ํธ๋ฅผ box๋ผ๋ ํ๋์ ์ปดํฌ๋ํธ๋ก ํตํฉํ๊ณ bgColor๋ผ๋ ์์ฑ์ ํตํด ๊ฐ๊ฐ ๋ฐฐ๊ฒฝ์์ ์ง์ ํด์ฃผ์๋ค. ์ฒ์์ ์ฝ๋๋ณด๋ค ์ฌ ์ฌ์ฉ์ฑ์ด ํฌ๊ฒ ์ฆ๊ฐํ๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
์์ Box ์ปดํฌ๋ํธ์ ๊ฐ์ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง Circle ์ปดํฌ๋ํธ ๋ง๋ค์ด๋ณด๊ธฐ
const Circle = styled.div`
background-color: ${(props) => props.bgColor};
width: 100px;
height: 100px;
border-radius: 50%;
`;
- border-radius ์์ฑ์ ์ ์ธํ๊ณ ๋ Box์ปดํฌ๋ํธ์ ๋ค๋ฅผ๊ฒ ์๋ค. Styled Components์์๋ ์ด๋ฐ ์ฝ๋์ ์ค๋ณต์ ๋ง๊ธฐ ์ํด html์ ํ๊ทธ ๋ฟ๋ง ์๋๋ผ, Styled Components๋ฅผ ํตํด ๋ง๋ ์ปดํฌ๋ํธ๋ ๋ค์ ์ ์ํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
- ์ฐจ์ด์ ์ด๋ผ๋ฉด styled(ComponentName)์ ๊ฐ์ด ์ปดํฌ๋ํธ ๋ช ์ ๊ดํธ ์์ ๋ฃ์ด์ค์ผ ํ๋ค๋ ๊ฒ ํ๋๋ฟ์ด๋ค.
const Box = styled.div`
background-color: ${(props) => props.bgColor};
width: 100px;
height: 100px;
`;
const Circle = styled(Box)`
border-radius: 50%;
`
const App = () => {
return (
<Wrapper>
<Box bgColor={"#cf6a87"} />
<Box bgColor={"#574b90"} />
<Circle bgColor={"black"} />
</Wrapper>
);
};
์ด๋ฐ ๋ฐฉ์์ผ๋ก!!
as ์์ฑ
๋ง์ฝ, box๊ฐ ์๋ป์ ๋ค๋ฅธ ๊ณณ์์ ๋ฒํผ์ผ๋ก ์ฌ์ฉํ๊ณ ์ถ์ด์ง ์๋ ์๋ค.
ํ์ง๋ง box๋ div๋ก ์ ์๋์ด ์๊ณ , ์๋์ box์ ๋์์ ์ฌ์ฉํ๋ฉด์ ์ค๋ณต๋๋ ์ฝ๋๋ ๋ง๋ค๊ณ ์ถ์ง ์๋ค๋ฉด?
๊ทธ๋ด ๋์๋ ์์ฃผ ๊ฐ๋จํ๊ฒ ์ปดํฌ๋ํธ ํ๊ทธ์ as์์ฑ์ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
const App = () => {
return (
<Wrapper>
<Box bgColor={"#cf6a87"} />
<Box as="button" bgColor={"#574b90"} />
<Circle bgColor={"black"} />
</Wrapper>
);
};
์ด๋ ๊ฒ ํ๊ณ ๋ธ๋ผ์ฐ์ ์์ ์์๋ฅผ ์ผ๋ณด๋ฉด div๊ฐ button์ผ๋ก ๋ฐ๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
์์ฑ ์ถ๊ฐ
required ์์ฑ์ ๊ฐ์ง inputํ๊ทธ๊ฐ ์ฌ๋ฌ ๊ฐ ํ์ํ ๊ฒฝ์ฐ, ๊ฐ๊ฐ์ ํ๊ทธ๋ง๋ค required์์ฑ์ ์ถ๊ฐํ๋ ๊ฒ์ ๋งค์ฐ ๋ฒ๊ฑฐ๋กญ๊ณ ๊ท์ฐฎ๋ค. ๋ง์ฝ ์์ ํ ์ผ์ด ์๊ธด๋ค๋ฉด, ํ๋์ฉ ๋ณ๊ฒฝํ๋ ๊ฒ์ ๋๋์ฑ ๊ท์ฐฎ๋ค.
styled Components์์๋ htmlํ๊ทธ์ ์์ฑ๋ ์ง์ ํ ์ ์๋ค.
const Input = styled.input.attrs({ required:true })`
background-color: orange;
margin-right: 5px;
`;
์ ๋๋ฉ์ด์
Styled COmponents๋ฅผ ํตํด ์ ๋๋ฉ์ด์ ์ ๋ฃ๊ธฐ ์ํด์ keyframes๋ฅผ import ํด์ผ ํ๋ค.
import {keyframes} from "styled-components";
circle์ ์๊น์ด ๋ฐ๋๋ ์ ๋๋ฉ์ด์ ์ ์ถ๊ฐํ๋ฉด
import {keyframes} from "styled-components";
const CircleAnimation = keyframes`
0% {
background-color:red;
}
33% {
background-color:green;
}
66%
{
background-color:blue;
}
100% {
background-color:red;
}
`;
const Circle = styled(Box)`
border-radius: 50%;
animation: ${CircleAnimation} 3s linear infinite;
`;
const App = () => {
return (
<Wrapper>
<Circle bgColor={"red"} />
</Wrapper>
);
};
์ฌ๊ธฐ์ ์ฃผ์! ์ ๋๋ฉ์ด์ ์ ์ปดํฌ๋ํธ๋ณด๋ค ๋จผ์ ์ ์ํด์ฃผ์ด์ผ ์ฑ๊ณต์ ์ผ๋ก ์ ๋๋ฉ์ด์ ์ด ์ ์ฉ๋๋ค.
์์๊ฐ ๋ฐ๋๋ค๋ฉด ์๋ฌ๋ฉ์์ง๊ฐ ๋จ๋ ์ฃผ์!
๊ฐ์ ์ ํ์
์ ํ์?
๋ง ๊ทธ๋๋ก ์ ํ์ ํด์ฃผ๋ ์์
์ด๋ฅผ ํตํด ํน์ ์์๋ค์ ์ ํํ์ฌ ์คํ์ผ์ ์ ์ฉํ ์ ์๊ฒ ๋๋ค.
๋จผ์ , ์ ํ์๋ฅผ ์์๋ณด๊ธฐ ์ํด Box์ปดํฌ๋ํธ ๋ด์ spanํ๊ทธ ํ๋๋ฅผ ์ถ๊ฐํด๋ณด์๋ค.
const Box = styled.div`
background-color: ${(props) => props.bgColor};
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
span {
background-color: black;
}
`;
const App = () => {
return (
<Wrapper>
<Box bgColor={"#cf6a87"}>
<span>๐</span>
</Box>
</Wrapper>
);
};
๊ฐ์ ์ ํ์
span์์ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ ธ์ ๋ (hover), ๋ฐฐ๊ฒฝ ์์์ ๋ฐ๊พธ๊ธฐ ์ํ ๋๊ฐ์ง ๋ฐฉ๋ฒ
// 1๋ฒ ๋ฐฉ๋ฒ
const Box = styled.div`
์๋ต...
span {
background-color: black;
}
span:hover {
background-color: white;
}
`;
// 2๋ฒ ๋ฐฉ๋ฒ
const Box = styled.div`
span {
background-color: black;
&:hover {
background-color: white;
}
}
`;