styled components
styled-components ๊ฐ๋
Automatic critical CSS : styled-components๋ ํ์ด์ง์ ์ค์ ๋ก ๋ ๋๋ง๋๋ ์ปดํฌ๋ํธ์๋ง ์คํ์ผ์ด ์ ์ฉ๋๋ฏ๋ก ๋ถํ์ํ CSS๋ฅผ ์ ๊ฑฐํ ์ ์๋ค.
No class name bugs : ๊ณ ์ ํ ํด๋์ค ์ด๋ฆ์ ์์ฑํ๋ค. ์ค๋ณต, ์ฒ ์ ์ค๋ฅ์ ๋ํด ๊ฑฑ์ ํ ํ์๊ฐ ์๋ค.
Easier deletion of CSS : ๋ชจ๋ ์คํ์ผ๋ง์ด ํน์ ๊ตฌ์ฑ ์์์ ์ฐ๊ฒฐ๋์ด ์์ด ๋ช ํํ๊ฒ ์ ์ ์๋ค. ๊ตฌ์ฑ ์์๊ฐ ์ฌ์ฉ๋์ง ์๊ณ ์ญ์ ๋๋ฉด ์คํ์ผ๋ ์ญ์ ๋๋ค.
Simple dynamic styling : props๋ฅผ ์ ๋ฌํ์ฌ ๋์ ์ผ๋ก ์คํ์ผ์ ๋ณ๊ฒฝํ ์ ์๋ค.
Painless maintenance : CSS๋ฅผ ์์ฑํ ํ์๊ฐ ์์ผ๋ฏ๋ก ์ ์ง ๊ด๋ฆฌ๊ฐ ์ฝ๋ค.
Automatic vendor prefixing : CSS๋ฅผ ํ์ฌ ํ์ค์ ๋ง๊ฒ ์์ฑํ๊ณ ์คํ์ผ ๊ตฌ์ฑ ์์๊ฐ ๋๋จธ์ง๋ฅผ ์ฒ๋ฆฌํ๋๋ก ํ ์ ์๋ค.
์ค์น
npm i styled-components
npm i -D @types/styled-components @swc/plugin-styled-components
vscode-styled-components extention ์ค์น
.swcrc ์ถ๊ฐ
{
"jsc": {
"experimental": {
"plugins": [
[
"@swc/plugin-styled-components",
{
"displayName": true,
"ssr": true
}
]
]
}
}
}
์ฌ์ฉ๋ฒ
๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
import styled from 'styled-components';
const Paragraph = styled.p`
color: #00f;
`;
export default function Greeting() {
return (
<Paragraph>Hello, world!</Paragraph>
);
}
import styled from 'styled-components';
const Paragraph = styled.p`
color: #00f;
string {
color: #f00;
}
`;
export default function Greeting() {
return (
<Paragraph>Hello, world<strong>!</strong></Paragraph>
);
}
์คํ์ผ๋ ์ปดํฌ๋ํธ์ ๋ค๋ฅธ ์คํ์ผ๋ ์ปดํฌ๋ํธ๋ฅผ ์์๋ฐ์์ ์ฌ์ฉํ ์ ์๋ค.
import styled from 'styled-components';
const Paragraph = styled.p`
color: #00f;
string {
color: #f00;
}
`;
const BigParagraph = styled(Paragraph)`
font-size: 2rem;
`;
export default function Greeting() {
return (
<BigParagraph>Hello, world<strong>!</strong></BigParagraph>
);
}
Last updated