Skip to content

Instantly share code, notes, and snippets.

@vonwao
Forked from pramendra/Styled-component-usage.md
Last active March 6, 2018 23:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vonwao/3c864dace94f29bafd6b86e23b5ae1d2 to your computer and use it in GitHub Desktop.
Save vonwao/3c864dace94f29bafd6b86e23b5ae1d2 to your computer and use it in GitHub Desktop.

Styled component use case

Styled component as standard react component

avoid creating middleware react component

before

const StyledIcon = styled(Icon)``;
export const HomeIcon = () => <StyledIcon uri="icn-home.svg" />;

after

export const CameraIcon = styled(Icon)``;
CameraIcon.defaultProps = {
  uri: 'btn-sell.svg',
};

advantage: can avoid creating react component in the middle

As default component

before

const StyledButton = styled.button`
  borderradius: 2;
  border: 0;
  background: none;
`;

export const Button = ({ children, onClick = () => {} }: buttonType) => (
  <StyledButton onClick={onClick}>{children}</StyledButton>
);

after

export const Button = styled.button`
  border: 0;
  background: none;
`;

advantage: code is simple and more readable

Inheritance

export const PlainButton = Display2.withComponent(CoreButton).extend`
  padding: 5px 10px;
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment