I'm creating a component for each svg icons. For that i'm providing the following properties for it,
export interface IconProperties{
width?: string | number;
height?: string | number;
size?: "small" | "medium" | "large";
onClick?: React.MouseEventHandler<SVGElement>;
disabled?: boolean;
}
Now to create Component for each icon, I'm doing
import * as React from "react";
import Attachment from "../../svg/attach.svg";
const AttachmentIcon = (props: IconProperties) => {
return (
<div>
<Attachment
width={props.width}
height={props.height}
onClick={props.onClick}
style={
{props.size}
}
/>
</div>
);
};
export default AttachmentIcon;
User will call icon component in the following way :
import * as React from "react";
import AttachmentIcon from "./iconComponents/AttachmentIcon";
const onClickOperation = (): void => {
alert("Icon Clicked!");
};
const DemoApp = () => {
return (
<div>
<AttachmentIcon
width="60px"
height="60px"
onClick={onClickOperation}
disabled={true}
size={"small"}
/>
</div>
);
};
export default DemoApp;
Here my properties is same for all the icons and I have to keep on repeating the same lines of code for each icon to make it as a component. How do I avoid duplicating same code for every icon.
Attachmentis already a component. Why do you need that wrapper?Attachmentdirectly, what's the point of this wrapper?Attachmentdirectly, but I have to add custom properties to it (like size, onClick etc). That's the reason I'm trying out to create a wrapper on top of it