[英]How can I create JSX elements from an array of React-Icon names?
import React from "react"
import {GiCard2Spades,GiCard3Spades} from "react-icons/gi"
const x = ["GiCard8Spades","GiCard9Spade"]
const y = x.map(item => <item />) //basically to get <GiCard8Spades /> elements
return(<div>{y}</div>)
I know I can right away make array manually with JSX items like but need other way at this situation.我知道我可以立即使用 JSX 项目手动创建数组,但在这种情况下需要其他方式。
If you're dead set on using strings, you could try如果你死心塌地使用字符串,你可以试试
import * as Icons from "react-icons/gi"
const x = ["GiCard8Spades","GiCard9Spade"];
return(<div>{x.map((item) => Icons[item])}</div>)
try this way试试这个方法
import React from "react"
import {GiCard2Spades,GiCard3Spades} from "react-icons/gi"
const x = [GiCard8Spades,GiCard9Spade];
return(<div>{x.map(item => item)}</div>)//basically to get <GiCard8Spades /> elements
This way you will create an array of JSX.Elements, not an array of strings这样,您将创建一个 JSX.Elements 数组,而不是字符串数组
Yea Im kinda stupid, should just used是的,我有点愚蠢,应该使用
import React from "react"
import {GiCard2Spades,GiCard3Spades} from "react-icons/gi"
const x = [GiCard8Spades,GiCard9Spades]
const y = x.map(item => React.createElement(item))
return(<div>{y}</div>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.