Reputation: 1792
I'm building a Skeleton component that accepts a prop called circleSizes
, which is an array of numbers to set the width and height of the Skeleton.
This is the component:
export const SkeletonHandler: React.FC<SkeletonHandlerProps> = ({
count = 1,
circle = false,
circleSizes = [50, 50],
}) => {
return (
<SkeletonWrapper count={count} circleSizes={circleSizes}>
<Skeleton count={count} circle={circle} />
</SkeletonWrapper>
);
};
My types.ts:
export type SkeletonHandlerProps = {
count?: number;
circle?: boolean;
circleSizes?: Array<number>;
};
I know that if I remove the ?
, the error will go away but I don't want this property to be required.
And my styled-components file:
export const SkeletonWrapper = styled.div<SkeletonHandlerProps>`
width: ${(circle) => (circle ? 'fit-content' : '100%')};
${({ circle, circleSizes }) =>
circle &&
css`
.react-loading-skeleton {
width: ${circleSizes[0]};
height: ${circleSizes[1]};
}
`}
`
What I have tried:
circleSizes = [50, 50]
circleSizes?: Array<number> | [50, 50];
width: ${Number(circleSizes[0])};
What I'm doing wrong?
Upvotes: 0
Views: 431
Reputation: 624
it complains because you are using the brackets operand on a variable that might be undefined
so try adding a validity check before circleSizes && circleSized[1]
Upvotes: 1