2 回答

TA貢獻1859條經驗 獲得超6個贊
在您定義為的對象中:
{
icon: "<TwitterIcon />",
title: "title 1",
desc: "desc 1",
}
不要使用"<TwitterIcon />"它總是返回一個字符串,而是使用TwitterIcon:
{
icon: TwitterIcon,
title: "title 1",
desc: "desc 1",
}
最后,在需要的地方調用它,如下所示:
const AnotherComponent = ({ ...data}) => {
return (
{data[1].map(item => (
<div><item.icon /></div> // HERE: check I'm calling item.icon as React Component
<div>{item.title}</div>
<div>{item.desc}</div>
))}
)
}
通過這種方式,您可以將圖標傳遞到您想要的任何地方,而不僅僅是傳遞字符串。因此,當您需要渲染時,您可以將其稱為組件。我在工作中經常這樣做。

TA貢獻1815條經驗 獲得超10個贊
我認為你應該直接傳遞對象中的圖標組件,如下所示:
const data = [
{
title: "some title",
desc: "some desc",
},
[
{
icon: <TwitterIcon />,
title: "title 1",
desc: "desc 1",
} ...
然后在index.js中你可以這樣做(這樣傳遞props會更清楚):
const Homepage = () => {
return (
<AnotherComponent data={data} />
)
}
現在在AnotherComponent.jsx中您可以執行以下操作:
const AnotherComponent = ({data}) => {
return (
{data[1].map(item => (
<div>{item.icon}</div>
<div>{item.title}</div>
<div>{item.desc}</div>
))}
)
}
添加回答
舉報