styled-components 설치
styled-components
와 @types/styled-components
를 함께 설치해준다.
yarn add styled-components
yarn add --dev @types/styled-components
기본 적용
import styled from "styled-components";
const App = () => {
return (
<div>
<StyledButton>color change</StyledButton>
</div>
);
}
const StyledButton = styled.button`
width: 200px;
height: 50px;
font-size: 16px;
`;
간단 예제
클릭 시 버튼의 색이 변경되는 예제
const App = () => {
const [color, setColor] = useState<string>("yellow");
const handleClick = () => {
if (color === "yellow") setColor("blue");
else setColor("yellow");
};
return (
<StyledButton color={color} onClick={handleClick}>
색 변경
</StyledButton>
);
};
const StyledButton = styled.button<{ color: string }>`
width: 200px;
height: 50px;
font-size: 16px;
background-color: ${({ color }) => color};
color: ${({ color }) => color === "blue" && "white"};
`;
'개발' 카테고리의 다른 글
react-hook-form + yup으로 효율적인 폼 관리하기 (0) | 2023.03.25 |
---|---|
nvm "PREFIX" environment variable 에러 발생 (0) | 2023.03.19 |
React + Typescript 프로젝트에 eslint, prettier 적용 (0) | 2023.02.05 |
React + Typescript 프로젝트 시작하기 (0) | 2023.01.10 |
macOS nvm 설치 (0) | 2023.01.04 |