I'm running into annoying typescript errors when trying to style my generic component like follows:
const StyledParentComponent = styled(ParentComponent)`
${GenericComponent} {
...
}
`;
It throws a bunch of errors about JSX not being assignable to some Interpolation type.
Here is a small example that produces the error:
import styled from "styled-components";
const ParentComponent = () => {
return (
<ParentComponentContainer>
<GenericComponent value={10} />
</ParentComponentContainer>
);
};
interface GenericComponentProps<T> {
value: T;
className?: string;
}
const GenericComponent = <T,>({
value,
className,
}: GenericComponentProps<T>) => {
console.log(value);
return (
<GenericComponentContainer>
Logging generic component value
</GenericComponentContainer>
);
};
const GenericComponentContainer = styled.div``;
const RegularComponent = () => {
return <RegularContainer></RegularContainer>;
};
const RegularContainer = styled.div``;
const ParentComponentContainer = styled.div`
// this is where it will throw the error
${GenericComponent} {
display: flex;
}
${RegularContainer} {
display: flex;
}
`;
I was running what the standard pattern is for dealing with this issue. It's important that I'm able to style it this way so that I can have generic components as children and be able to style them with the above pattern.
I think using React Component in
styled.div()is very unusual. I've never seen it done before. How about we try this approach instead?First, forward the className props to
GenericComponentContainer.And then, styling
GenericComponentwith styled.