I have a Sidebar component which passes unique icons into a SidebarRow child component via props.
import SidebarRow from './SidebarRow';
import {
CogIcon,
UsersIcon
} from '@heroicons/react/solid';
const Sidebar: React.FC = () => {
return (
<div className="p-2 mt-5 max-w-5xl xl:min-w-lg">
<SidebarRow src="" title="Tom Mc" />
<SidebarRow Icon={UsersIcon} title="Friends" />
<SidebarRow Icon={CogIcon} title="Account" />
</div>
)
}
export default Sidebar;
Within the SidebarRow component an interface defines the incoming props. Here I am attempting to conditionally render either an image or an Icon, depending on which is passed in.
import React from "react";
interface SidebarRowProps {
src?: string
Icon?: React.FC
title: string
};
const SidebarRow: React.FC<SidebarRowProps> = ({ src, Icon, title }) => {
return (
<div className="">
{src && (
<img className="rounded-full" src={src} alt="" width="30" height="30" />
)}
{Icon && (
<Icon className="h-8 w-8 text-blue-500" />
)}
<p className="hidden sm:inline-flex font-medium">{title}</p>
</div>
)
};
export default SidebarRow;
I am receiving the below error for the className attribute on the Icon component:
Type '{ className: string; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'.
Property 'className' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.ts(2322)
(JSX attribute) className: string
How can I define the Icon type so that the className property does not throw this error?
Thank you!
Icon?: React.SVGProps<SVGSVGElement>, if found in the source code of your icon library this type, className does not exists on typeReact.FCReact.ComponentTypeinstead ofReact.FC. Example:Icon?: React.ComponentType<React.HtmlHTMLAttributes<HTMLElement>>. You can changeReact.HtmlHTMLAttributes<HTMLElement>to the Props Type of your Icon.