# with npm
npm install --save styled-components
# with yarn
yarn add styled-components
# Web(for React)
npm install --save-dev @types/styled-components
# React Native
npm install --save-dev @types/styled-components @types/styled-components-react-native
npm install --save-dev babel-plugin-styled-components
{
"plugins": ["babel-plugin-styled-components"]
}
import React from 'react';
import {Button} from '@ant-design/react-native';
import styled, {css} from 'styled-components/native';
const ExamStyledComponent = () => {
const [text, onChangeText1] = React.useState('');
return (
<>
<StyledText length={text.length}>StyledView Components</StyledText>
<StyledButton type="primary"> test Button </StyledButton>
<StyledTextInput onChangeText={onChangeText1} value={text} />
<StyledTextInput inputColor="blue" bold />
</>
);
};
interface StyledTextProps {
readonly length?: number;
}
const StyledText = styled.Text<StyledTextProps>`
padding: 50px;
background-color: yellow;
text-align: center;
`;
const StyledButton = styled(Button)`
margin: 30px;
background-color: blue;
`;
interface StyledTextInputProps {
readonly inputColor?: string;
readonly bold?: boolean;
}
const StyledTextInput = styled.TextInput<StyledTextInputProps>`
padding: 20px;
margin: 10px;
color: ${props => props.inputColor || 'palevioletred'};
background: papayawhip;
border: none;
border-radius: 3px;
${props =>
props.bold &&
css`
// props 를 쓰지 않는다면 사실 굳이 css 로 감쌀 필요는 없음
background: #523123;
border: solid 2px;
`}
`;
export default ExamStyledComponent;
Styled-Component 에서 사용되는 styled(Button)~~css~~
와 같은 형태는
Tagged templates 라는 javascript template literals 문법이다.
react native 에서 styled-components의 keyframe 은 사용 불가능함 (RN Animation 참고할 것)
(참고 사이트)styled-components 를 사용하는 8가지 이유 -번역(https://analogcoding.tistory.com/181)