3

react-icons在“卡片组件”中有一个通过道具的列表。在我的 Card 组件的渲染方法中,我有这样的东西:

render() {
  ...
  {
     this.props.icons.map(icon => {
        return (
          <div className="icon-square">
            /*   What should I do here so I render the "icon" variable"   */
          </div>
        )
     })
   }
}

注意:该列表由 react-icons 组成,它们本身就是 React 组件。

我尝试了很多东西,但我不太清楚如何渲染图标。如果有人可以帮助我,那就太棒了。谢谢

4

4 回答 4

3

如果图标是一个反应组件,那么:

this.props.icons.map(Icon => {
        return (
          <div className="icon-square">
            <Icon/>
          </div>
        )
     })
于 2021-01-20T19:21:34.400 回答
2

假设您已经通过了一个图标列表,例如

import { FaBeer, FaBug, FaAnchor, FaCoffee } from 'react-icons/fa';

const icons = [FaBeer, FaBug, FaAnchor, FaCoffee];

ReactDOM.render(
    <CardComponent icons = {icons} />,
    document.querySelector('root')
};

CardComponent.js

class CardComponent extends React.Component{
...

render() {

  // Icon is a Component
  return (
    this.props.icons.map((Icon) => {
     return <Icon />
    });
  )

 }
}
于 2021-01-20T19:33:19.957 回答
1

这是使用图标的两个区别,如果您作为 JSX 传递,您应该使用{icon} 但是如果您作为组件传递,您应该像这样使用<Icon/>

于 2021-01-20T19:27:40.370 回答
0

我认为包装你只需要放图标是{}

render() {
  ...
  {
     this.props.icons.map(icon => {
        return (
          <div className="icon-square">
            {icon}
          </div>
        )
     })
   }
}
于 2021-01-20T19:17:29.923 回答