简体   繁体   中英

How to use React Icons with an icon inside the tag

I'm trying to use react icons without import the icon of the lib. Is it possible to call the icon and pass the name like <Icon name="FiMenu" /> or <Icon name="FiArrowRight" /> ?

Nothing happened, looks like property Icon does not exists from react-icons.

There is no such component called Icon in the library or in React so you do need to individually import the components.

Follow the documentation: https://github.com/react-icons/react-icons The basic usage is:

import { FaBeer } from 'react-icons/fa';

class Question extends React.Component {
  render() {
    return <h3> Lets go for a <FaBeer />? </h3>
  }
}

If you really want to understand what is happening I suggest having a look through the source code.

The not best practice, but you can also create oan object with key (name of icon) and value the icon. So after this import of your icons. Example:

import {FaBeer} from "react-icons/fa"
const icons = {
  "FaBeer": FaBeer
}
export const Icon ({name, ...props} ) => {
  const IconComponent = icons[name]
  return <IconComponent {...props} />
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM