如何使用 font Awesome 与 map() 进行反应

发布于 2025-01-10 22:49:46 字数 1316 浏览 1 评论 0原文

我对 RectJs 非常陌生,所以这可能是一个非常初学者的问题,但是,我有一个名为 MyArr 的对象数组,如下所示:

const MyArr =[
   {picture:'faUser' ,  text:'something'},
   {picture:'faUser' ,  text:'something'} ]
export default MyArr

我想在另一个组件中使用“图片”的值来声明 #FontAwesome 中的哪个图标应该显示。 所以我制作了另一个组件,在其中将对象数组作为 props 值传递到目的地,就像这样

import React, { Component } from 'react'
import MyArr from './MyArr'
import ShowFontAwesome from './ShowFontAwesome'

 class Body extends Component {

  render() {
   return(
        <div>
          <ShowFontAwesome myValue={MyArr} />          
        </div>
)
}}
export default Body

,所以名为“ShowFontAwesome”的组件是我要显示结果的地方; 最后这是组件的代码

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import * as FontAwesome from '@fortawesome/free-solid-svg-icons';
import React, { Component } from 'react'

 class ShowFontAwesome extends Component {
   constructor(props){
     super(props);}
render() {
    return (
<div>
{this.props.myValue.map(
          (myValues, index)=>{
            return(
<div>
<FontAwesomeIcon icon={`FontAwesome.${myValues.picture}`} />
</div>
)})
</div>
)}}
export default ShowFontAwesome

,它在控制台中记录此错误 ** 找不到图标 {prefix: 'fas', iconName: 'FontAwesome.faUser'} **

I am very new in RectJs and so this may be a very beginner question, However, I have got an array of objects called MyArr like this:

const MyArr =[
   {picture:'faUser' ,  text:'something'},
   {picture:'faUser' ,  text:'something'} ]
export default MyArr

I want to use the value of "picture" in another component to declare which icon from #FontAwesome should be shown.
so I made another component in which I pass that Array of Objects as a props value to the destination, like this

import React, { Component } from 'react'
import MyArr from './MyArr'
import ShowFontAwesome from './ShowFontAwesome'

 class Body extends Component {

  render() {
   return(
        <div>
          <ShowFontAwesome myValue={MyArr} />          
        </div>
)
}}
export default Body

so the component called "ShowFontAwesome" is where I am going to show the result; this is the component's code

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import * as FontAwesome from '@fortawesome/free-solid-svg-icons';
import React, { Component } from 'react'

 class ShowFontAwesome extends Component {
   constructor(props){
     super(props);}
render() {
    return (
<div>
{this.props.myValue.map(
          (myValues, index)=>{
            return(
<div>
<FontAwesomeIcon icon={`FontAwesome.${myValues.picture}`} />
</div>
)})
</div>
)}}
export default ShowFontAwesome

finally, it logs this error in the console ** could not find icon {prefix: 'fas', iconName: 'FontAwesome.faUser'} **

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2025-01-17 22:49:46

更新:

首先将其添加到您的组件中:

import {fas} from '@fortawesome/free-solid-svg-icons'
import { library } from "@fortawesome/fontawesome-svg-core";

library.add(fas);

并将图片值从“faUser”更改为“user”,因此您的数组将如下所示:

const MyArr =[
   {picture:'user' ,  text:'something'},
   {picture:'user' ,  text:'something'} ]
export default MyArr

然后像这样使用它:

<FontAwesomeIcon icon={myValues.picture} />

Update:

first add this to your component:

import {fas} from '@fortawesome/free-solid-svg-icons'
import { library } from "@fortawesome/fontawesome-svg-core";

library.add(fas);

and change the picture value from "faUser" to just "user", so you array will be like this:

const MyArr =[
   {picture:'user' ,  text:'something'},
   {picture:'user' ,  text:'something'} ]
export default MyArr

and then use it like this:

<FontAwesomeIcon icon={myValues.picture} />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文