OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to pass props to styled component without converting them to transient props?

  • Thread starter Thread starter Toesmash
  • Start date Start date
T

Toesmash

Guest
I have created a Text component which receives couple of props. These props are passed to the styled component, where styles are applied. I dont want to pass props to the DOM, I only want to access them inside styled component.

Converting props to transient props works, but it creates repeating code which I think makes the whole component unnecessarily bigger.

I would like to know, how to pass some props to my styled component without passing them to the DOM and without having to redefine every prop. I tried using shouldForwardProps but I cant get it to work with Typescript.

Code:
type Props = {
  children: React.ReactNode;
  size: number;
  height: number;
  color: string;
};

const StyledText = styled.p<{ $color: string; $size: number; $height: number }>`
  color: ${({ $color }) => $color};
  font-size: ${({ $size }) => `${$size}px;`};
  line-height: ${({ $height }) => `${$height}px;`};
  ...
`;

const Text: React.FC<Props & React.HTMLAttributes<HTMLParagraphElement>> = ({ children, size, height, color, ...rest }) => {
  return (
    <StyledText $size={size} $height={height} $color={color} {...rest}>
      {children}
    </StyledText>
  );
};

<p>I have created a <code>Text</code> component which receives couple of props. These props are passed to the styled component, where styles are applied. I dont want to pass props to the DOM, I only want to access them inside styled component.</p>
<p>Converting props to transient props works, but it creates repeating code which I think makes the whole component unnecessarily bigger.</p>
<p>I would like to know, how to pass some props to my styled component without passing them to the DOM and without having to redefine every prop. I tried using <code>shouldForwardProps</code> but I cant get it to work with Typescript.</p>
<pre class="lang-js prettyprint-override"><code>type Props = {
children: React.ReactNode;
size: number;
height: number;
color: string;
};

const StyledText = styled.p<{ $color: string; $size: number; $height: number }>`
color: ${({ $color }) => $color};
font-size: ${({ $size }) => `${$size}px;`};
line-height: ${({ $height }) => `${$height}px;`};
...
`;

const Text: React.FC<Props & React.HTMLAttributes<HTMLParagraphElement>> = ({ children, size, height, color, ...rest }) => {
return (
<StyledText $size={size} $height={height} $color={color} {...rest}>
{children}
</StyledText>
);
};
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top