Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Last active July 16, 2020 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/e9e6680fc04e1efb7466bdb91d110a6c to your computer and use it in GitHub Desktop.
Save whoisryosuke/e9e6680fc04e1efb7466bdb91d110a6c to your computer and use it in GitHub Desktop.
Typescript / Styled Component / React Native - How to type Styled Components
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