react-icons icon indicating copy to clipboard operation
react-icons copied to clipboard

`Type 'IconType' is not assignable to type 'ReactNode'`

Open nikableh opened this issue 4 years ago • 8 comments

Describe the bug I am using TypeScript for my project. I am trying to create a wrapper for icons from this package.

Expected behavior I should be able to pass in icons of Type IconType into my Sidebarcon() component for them to be wrapped with <div className="sidebar-icon">.

Screenshots Screenshot from 2022-07-09 17-27-19

Desktop (please complete the following information):

  • Device: Fedora Linux 36
  • Browser Firefox

nikableh avatar Jul 09 '22 14:07 nikableh

Hello, fellow react-icons user here.

IconTypes are like react components, not elements. They are meant to be rendered like so: <Icon />.

What I usually do is alias them to a capital-letter name when destructuring (that'd be { icon: Icon }: ... in your example) then you can do <div> <Icon /> </div>

Enjoy!

marcelltoth avatar Jul 11 '22 12:07 marcelltoth

you are welcome...

marcelltoth avatar Jul 12 '22 11:07 marcelltoth

I solved the issue by doing something else – making the function accept JSX.Element instead of IconType.

Sorry for not saying anything and silently closing the issue.

nikableh avatar Jul 12 '22 11:07 nikableh

I solved the issue by doing something else – making the function accept JSX.Element instead of IconType.

Sorry for not saying anything and silently closing the issue.

How do i do that?

alikulaOwen avatar Nov 10 '22 03:11 alikulaOwen

@alikulaOwen instead of the function accepting the icon parameter as IconType, it instead accepts it as a function that returns JSX.Element. Example: Instead of const funcName = ({icon}) => { return (<div>{icon}</div>) }

It should be const funcName = ({Icon}) => { return (<div> <Icon /> </div>) }

Hope this helps. :)

clementgyimah avatar Nov 13 '22 17:11 clementgyimah

Thanks

alikulaOwen avatar Nov 28 '22 09:11 alikulaOwen

Hello, fellow react-icons user here.

IconTypes are like react components, not elements. They are meant to be rendered like so: <Icon />.

What I usually do is alias them to a capital-letter name when destructuring (that'd be { icon: Icon }: ... in your example) then you can do <div> <Icon /> </div>

Enjoy!

I like this method, but whenever I pass icon as a prop the following error comes up: Type 'Element' is not assignable to type 'IconType'. Type 'ReactElement<any, any>' provides no match for the signature '(props: IconBaseProps): Element'.ts(2322)

how could I solve this?

YO-SC avatar May 22 '23 20:05 YO-SC

Hello, fellow react-icons user here. IconTypes are like react components, not elements. They are meant to be rendered like so: <Icon />. What I usually do is alias them to a capital-letter name when destructuring (that'd be { icon: Icon }: ... in your example) then you can do <div> <Icon /> </div> Enjoy!

I like this method, but whenever I pass icon as a prop the following error comes up: Type 'Element' is not assignable to type 'IconType'. Type 'ReactElement<any, any>' provides no match for the signature '(props: IconBaseProps): Element'.ts(2322)

how could I solve this?

never mind guys, I just used the function method: icon= {() => <ReactIconHere/>}

although I would like to pass just the icon itself

YO-SC avatar May 22 '23 20:05 YO-SC