question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add icon name prop

See original GitHub issue

Does it not make sense to have a reusable icon component of our own that utilizes react-feather, and therefore be able to do something like <Icon icon={this.props.icon} />?

I’m aware that we can always pass an icon like <Camera /> down as a prop, but I’m wondering if it’s possible to this just by the name of the icon.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:14
  • Comments:19

github_iconTop GitHub Comments

25reactions
kraenhansencommented, Jul 11, 2020

For anyone interested, I just wrote this functional component which will render an icon by name (in TypeScript):

import React from "react";
import * as icons from "react-feather";

export type IconName = keyof typeof icons;

export type IconProps = {
  name: IconName;
} & icons.Props;

export function Icon({ name, ...rest }: IconProps) {
  const IconComponent = icons[name];
  return <IconComponent {...rest} />;
}
8reactions
Kosai106commented, Jul 23, 2019

You could always abstract this kind of behavour, but otherwise doing the following would allow you a similar experience (Almost).

const IconTag = Icon[this.props.icon];

<IconTag />
Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - How to pass an icon as a prop? - Stack Overflow
I'm using props and I want to have "optionText" and "optionIcon" the 1st one I'm able to add but I'm not able to...
Read more >
Passing icons as props in a consistent way using React
The first thing we see are IconTypeProps which are the props your generic icons should take in. If we're referring to the initial...
Read more >
Add Icons with React | Font Awesome Docs
There are a few ways of adding icons to a React project. Choose the option that works for your project, and then add...
Read more >
Icon - React Native Elements
Custom Icon Fonts​ ... Register your own custom icons by calling registerCustomIconType('customid', customFont) . Create a custom font by following the ...
Read more >
Icons · React Native Paper - Open Source
You can pass the name of an icon from MaterialCommunityIcons . This will use the react-native-vector-icons library to display the icon. Example: < ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found