๐ styled-components
์ปดํฌ๋ํธ ์คํ์ผ๋ง์ ๋ ๋ค๋ฅธ ํจ๋ฌ๋ค์์ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ ์์ ์คํ์ผ์ ์ ์ธํ๋ ๋ฐฉ์(CSS-in-JS)์ด๋ค. ์ด์ ๊ด๋ จ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ข ๋ฅ๋ ์ ๋ง ๋ง๋ค.
CSS-in-JS ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์์ ๊ฐ๋ฐ์๋ค์ด ๊ฐ์ฅ ์ ํธํ๋ styled-components์ ๋ํด ์์๋ณด์!
๋จผ์ ์ค์น๋ถํฐ ํด ์ค๋ค.
$ yarn add styled-components
โ๐ค styled-components ์ฌ์ฉ ์ ์ฅ์ ์?
๐ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ ํ๋์ ์คํ์ผ๊น์ง ์์ฑํ ์ ์์ด .css ๋๋ .scss ํ์ฅ์๋ฅผ ๊ฐ์ง ์คํ์ผ ํ์ผ์ ๋ฐ๋ก ๋ง๋ค์ง ์์๋ ๋๋ค!
import styled, { css } from 'styled-components';
const Box = styled.div`
/* props๋ก ๋ฃ์ด ์ค ๊ฐ์ ์ง์ ์ ๋ฌํด ์ค ์ ์์ต๋๋ค. */
background: ${props => props.color || 'blue'};
padding: 1rem;
display: flex;
`;
const Button = styled.button`
background: white;
color: black;
border-radius: 4px;
padding: 0.5rem;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
font-size: 1rem;
font-weight: 600;
/* & ๋ฌธ์๋ฅผ ์ฌ์ฉํ์ฌ Sass์ฒ๋ผ ์๊ธฐ ์์ ์ ํ ๊ฐ๋ฅ */
&:hover {
background: rgba(255, 255, 255, 0.9);
}
/* ๋ค์ ์ฝ๋๋ inverted ๊ฐ์ด true์ผ ๋ ํน์ ์คํ์ผ์ ๋ถ์ฌํด ์ค๋๋ค. */
${props =>
props.inverted &&
css`
background: none;
border: 2px solid white;
color: white;
&:hover {
background: white;
color: black;
}
`};
& + button {
margin-left: 1rem;
}
`;
const StyledComponent = () => (
<Box color="black">
<Button>์๋
ํ์ธ์</Button>
<Button inverted={true}>ํ
๋๋ฆฌ๋ง</Button>
</Box>
);
export default StyledComponent;import React, { Component } from 'react';
import StyledComponent from './StyledComponent';
class App extends Component {
render() {
return (
<div>
<StyledComponent />
</div>
);
}
}
export default App; 
โ๐ค styled-components์ ์ผ๋ฐ classNames๋ฅผ ์ฌ์ฉํ๋ CSS/Sass์ ๋น๊ต ์ ์ฅ์ ์?
๐ props ๊ฐ์ผ๋ก ์ ๋ฌํด ์ฃผ๋ ๊ฐ์ ์ฝ๊ฒ ์คํ์ผ์ ์ ์ฉํ ์ ์๋ค๋ ๊ฒ!