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

Add icon name prop

Open ffpetrovic opened this issue 5 years ago • 19 comments

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.

ffpetrovic avatar Jul 19 '19 05:07 ffpetrovic

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 />

Kosai106 avatar Jul 23 '19 22:07 Kosai106

@Kosai106 I've created a component like that in every project where I've used react-feather. I agree with @ffpetrovic that baking that into the library would be a good idea.

SeanMcP avatar Jul 31 '19 13:07 SeanMcP

I just realised that my method wouldn't work if you're using TypeScript anyway.

Element implicitly has an 'any' type because type 'typeof import("/Users/oesterkilde/projects/[PROJECT-NAME]/node_modules/react-feather/dist/index")' has no index signature

Kosai106 avatar Aug 12 '19 17:08 Kosai106

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 />

Thanks for the suggestion @Kosai106, this solved my issue :)

LukyVj avatar Aug 22 '19 12:08 LukyVj

I know this is a bit old but in case anyone needs it I managed to create a component that lazy loads the icons based on the name (you'll need to use lowercase) and works with Typescript:


interface DpFeatherIconProps {
  icon: string;
}

export default (props: DpFeatherIconProps) => {
  const IconTag = React.lazy(() => import(`react-feather/dist/icons/${props.icon}.js`));

  return (
    <Suspense fallback={null}>
      <IconTag />
    </Suspense>
  );
};

agustingabiola avatar May 04 '20 01:05 agustingabiola

@agustingabiola Thank you for this workaround. However, the use of lazy/Suspense seems to cause some "blinking" on some loads. Are there any other workarounds?

This is what I've done to prevent layouts from jumping around as much:

<Suspense
  fallback={
    <Loader
      color="white"
      className={props.className}
      size={props.size || 12}
    />
  }
>
...
</Suspense>

kungpaogao avatar Jul 04 '20 06:07 kungpaogao

@kungpaogao For the fallback, just use an empty div with the same dimensions as your icon - This should prevent any layout shifting.

Kosai106 avatar Jul 04 '20 12:07 Kosai106

@Kosai106 yup, that’s what I’m doing already, but it doesn’t solve the “blinking” that happens when the icon is loaded

kungpaogao avatar Jul 04 '20 14:07 kungpaogao

@Kosai106 yup, that’s what I’m doing already, but it doesn’t solve the “blinking” that happens when the icon is loaded

@kungpaogao Use memo for the component alongside with an element with the same dimensions for fallback. For example if you have a blue cross of 16px you can check that if all props are equal return the same rendered component

agustingabiola avatar Jul 05 '20 04:07 agustingabiola

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} />;
}

kraenhansen avatar Jul 11 '20 13:07 kraenhansen

@kraenhansen This is exactly what I'm looking for! Thank you!

kungpaogao avatar Jul 11 '20 15:07 kungpaogao

It would be very interesting to be able to pass on the name as property.

osvaldokalvaitir avatar Jul 27 '20 15:07 osvaldokalvaitir

Hello :wave:

Nice solution from @kraenhansen, however, using this on next@10 & TypeScript result with the following error:

Server Error
Error: Element type is invalid: expected a string (for built-in components) 
or a class/function (for composite components) but got: undefined.
You likely forgot to export your component from the file it's defined in, 
or you might have mixed up default and named imports.
Screenshot 2020-11-05 at 10 49 26

If anyone got a clue or a solution for this.

Below, my current files:

Icon/Icon.tsx

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} />;
}

And where I use the icon:

import { Icon } from "../Icon/Icon";

...
...
...
{icon && <Icon name={icon} />}

From what I understand, the exports/imports seems to be wrong somehow, I've tried several ways to fix this but none works, any hint would be appreciated 😄

LukyVj avatar Nov 05 '20 09:11 LukyVj

@LukyVj It works just fine in Next.js from what I can tell. https://codesandbox.io/s/hopeful-franklin-xr1fe?file=/src/pages/index.tsx

From the error message you're sharing it seems to come from somewhere else.

Kosai106 avatar Nov 05 '20 12:11 Kosai106

@LukyVj It works just fine in Next.js from what I can tell. codesandbox.io/s/hopeful-franklin-xr1fe?file=/src/pages/index.tsx

From the error message you're sharing it seems to come from somewhere else.

Thanks for answering!

So, yeah, I've tried your code, works well when I pass the name directly as a string Icon name="box" works just fine.

But, when I tried to pass it through a prop, it doesn't work, e.g:

Where I define the needed icon

  <Button
        onClick={() => {
          toggleOpen(!open);
        }}
        primary
        className="mb-16"
        icon="Box"
      />

The button code:

{icon && <Icon name={icon} />}

So, I've somehow understood why this doesn't work based on your comment, turns out my component get rendered several times including one where the icon is undefined, I suppose that's what's causing the error :D

LukyVj avatar Nov 05 '20 12:11 LukyVj

@LukyVj It works just fine in Next.js from what I can tell. codesandbox.io/s/hopeful-franklin-xr1fe?file=/src/pages/index.tsx From the error message you're sharing it seems to come from somewhere else.

Thanks for answering!

So, yeah, I've tried your code, works well when I pass the name directly as a string Icon name="box" works just fine.

But, when I tried to pass it through a prop, it doesn't work, e.g:

Where I define the needed icon

  <Button
        onClick={() => {
          toggleOpen(!open);
        }}
        primary
        className="mb-16"
        icon="Box"
      />

The button code:

{icon && <Icon name={icon} />}

So, I've somehow understood why this doesn't work based on your comment, turns out my component get rendered several times including one where the icon is undefined, I suppose that's what's causing the error :D

Have you figured out a solution for this?

davidperklin avatar Dec 07 '20 15:12 davidperklin

@kraenhansen thanks!!

GCobo avatar Dec 13 '20 07:12 GCobo

@kraenhansen doesn't that solution have the problem of all 280 icons being included into your webpack bundle so that you can pick components dynamically? Tree shaking wouldn't be able to work in this case right?

lawnchamp avatar Jul 29 '21 20:07 lawnchamp

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} />;
}

I have modified as follows, in case it's useful to anyone.

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

export type IconName = keyof typeof Icons;

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

const Icon: React.FC<IconProps> = ({ name, ...rest }) => {
    const IconComponent = Icons[name];
    return <IconComponent {...rest} />;
};

export default Icon;

Usage is <Icon name="Download" />

BenSampo avatar Dec 09 '22 10:12 BenSampo