Last active
July 16, 2020 23:54
-
-
Save whoisryosuke/e9e6680fc04e1efb7466bdb91d110a6c to your computer and use it in GitHub Desktop.
Typescript / Styled Component / React Native - How to type Styled Components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ButtonProps = { | |
primary: boolean; | |
}; | |
const Button = styled.TouchableOpacity<ButtonProps>` | |
opacity: ${(props) => (props.primary ? 0.5 : 1)}; | |
`; | |
// Or HTML version | |
interface TitleProps { | |
readonly isActive: boolean; | |
}; | |
const Title = styled.h1<TitleProps>` | |
color: ${props => props.isActive ? props.theme.colors.main : props.theme.colors.secondary}; | |
` | |
// or Custom Component version with inline type | |
const NewHeader = styled(Header)<{ customColor: string }>` | |
color: ${props => props.customColor}; | |
` | |
// or Custom Component version with interface | |
interface NewHeaderProps { | |
readonly customColor: string; | |
}; | |
const NewHeader = styled(Header)<NewHeaderProps>` | |
color: ${props => props.customColor}; | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment