Take this simple situation:
I have a container, with an h1 nested inside of it. I want to pass some props into both elements that will change their colour depending on a light / dark theme; eg: white text / black bg || black text / white bg
Problem: it would appear the props (color & text) being passed to the styled components are not cascading in the way that I would assume they would.
Simple example:
export function Heading(props) {
return (
<Container {...props}> // I would expect the props obj to be available by nested Heading el
<Heading>
{props.text}
</Heading>
</Container>
)
}
// styled components
export const Container = styled.div`
padding: 20px;
background-color: ${props => props.color === dark ? "black" : "white";
`;
export const Heading = styled.h1`
background-color: ${props => props.color === dark ? "white" : "black"; // does not take affect
`;
Instead I have to do:
<Container {...props}> // Pass to container
<Heading {...props}> // Pass to heading el
{props.text}
</Heading>
</Container>
Question:
Can I get the Heading
to pick up the {...props}
passed to Container
without declaring it twice?
Or am I misunderstanding how styled components are supposed to behave?