Out of sheer curiosity, I've been incorporating styled-components into my React application. Within this framework, I make use of the recommended theme to ensure consistency in colors and sizes throughout.
Currently, my approach looks something like this:
export const TestComponent = styled.div`
color: ${({ theme }) => theme.white};
background-color: ${({ theme }) => theme.white};
border: 1px solid ${({ theme }) => theme.white};
display: ${({ check }) => check ? 'block' : 'none'};
`;
Here, I'm tapping into the theme provided by the ThemeProvider
, as well as performing an additional check based on an external component.
Moving forward, I find myself pondering why I can't simply do it like this instead:
export const TestComponent = styled.div`${({ theme, check }) => (css`
color: ${theme.white};
background-color: ${theme.white};
border: 1px solid ${theme.white};
display: ${check ? 'block' : 'none'};
`)`;
Wouldn't this alternative method be more convenient and straightforward? If so, what might be preventing them from promoting this practice? I worry that there could be significant drawbacks associated with this approach that I'm failing to recognize at the moment.