Created
March 10, 2020 19:01
-
-
Save Jimmydalecleveland/6f038fb4410f3eb88ce606ffc4ffb1df to your computer and use it in GitHub Desktop.
Example usage of styled components with theme providers
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
// First example: using a callback for every line to access theme | |
export const PretendContainer1 = styled.div` | |
background-color: ${({ theme }) => theme.colors.primary}; | |
color: ${({ theme }) => theme.colors.secondary}; | |
padding: ${({ theme }) => theme.spacing.medium}; | |
margin-bottom: ${({ theme }) => theme.spacing.small}; | |
`; | |
// Second example: using a single call back that returns `css` for | |
// a single reusable theme prop | |
export const PretendContainer2 = styled.div` | |
${({ theme }) => css` | |
background-color: ${theme.colors.primary}; | |
color: ${theme.colors.secondary}; | |
padding: ${theme.spacing.medium}; | |
margin-bottom: ${theme.spacing.small}; | |
`}; | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the first or second example better for a component that uses a
theme
prop more than a couple times. Are there any inefficiencies to having several callbacks (First example) rather than a single (Second example), and are there any differences to using a callback that returnscss
that should be known?