Styled-Components
Pure css의 문제점에 대해 살펴보고, 해당 문제들을 해결해주면서 좀 더 리액트답게 styling 코드를 작성할 수 있도록 도와주는 styled-components 라이브러리에 대해 배워봅시다.
Last updated
Pure css의 문제점에 대해 살펴보고, 해당 문제들을 해결해주면서 좀 더 리액트답게 styling 코드를 작성할 수 있도록 도와주는 styled-components 라이브러리에 대해 배워봅시다.
Last updated
npm i styled-componentsconst Title = styled.h1`
font-size: 1.5em;
text-align: center;
color: palevioletred;
`;const Button = styled.button`
background: ${props => props.primary ? "palevioletred" : "white"};
color: ${props => props.primary ? "white" : "palevioletred"};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
render(
<div>
<Button>Normal</Button>
<Button primary>Primary</Button>
</div>
);const Button = styled.button`
color: palevioletred;
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
// A new component based on Button, but with some override styles
const TomatoButton = styled(Button)`
color: tomato;
border-color: tomato;
`;
render(
<div>
<Button>Normal Button</Button>
<TomatoButton>Tomato Button</TomatoButton>
</div>
);import { Link } from "react-router-dom";
const StyledLink = styled(Link)`
color: palevioletred;
font-weight: bold;
`;
render(
<div>
<Link>Unstyled, boring Link</Link>
<br />
<StyledLink>Styled, exciting Link</StyledLink>
</div>
);const Input = styled.input`
padding: 0.5em;
margin: 0.5em;
color: ${props => props.inputColor || "palevioletred"};
background: papayawhip;
border: none;
border-radius: 3px;
`;
// Render a styled text input with the standard input color, and one with a custom input color
render(
<div>
<Input defaultValue="@probablyup" type="text" />
<Input defaultValue="@geelen" type="text" inputColor="rebeccapurple" />
</div>
);// Create a Wrapper component that'll render a <section> tag with some styles
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
.title {
font-size: 1.5em;
text-align: center;
color: palevioletred;
}
`;
// Use Title and Wrapper like any other React component – except they're styled!
render(
<Wrapper>
<h2 className="title">제목</div>
</Wrapper>
);