如何使用 font Awesome 与 map() 进行反应
我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
首先将其添加到您的组件中:
并将图片值从“faUser”更改为“user”,因此您的数组将如下所示:
然后像这样使用它:
Update:
first add this to your component:
and change the picture value from "faUser" to just "user", so you array will be like this:
and then use it like this: