【发布时间】:2021-06-03 03:23:25
【问题描述】:
我正在使用带有 '@fluentui/react-northstar' 库的图标组件,如下所示
import { QnaIcon } from '@fluentui/react-northstar'
const Example1 = () => (
<>
<QnaIcon size="large" />
</>
)
现在我必须在图标名称来自类似这样的道具时动态加载图标组件
import React from 'react'
import { Button, TeamCreateIcon } from '@fluentui/react-northstar'
const Example2 = ({iconName}) => {
const MyIcon = <iconName /> ; //here I need to convert string to component
return (
<div >
<Button icon={<TeamCreateIcon />} title="Create" />
// Here I need set it
<Button icon={<MyIcon />} title="Create" />
</div>
)}
<Example2 iconName='QnaIcon' />
【问题讨论】:
-
你能在 props 中传递图标本身而不只是图标名称吗?
-
@squillman 我必须从 json 传递图标名称,它是可配置的
-
我只是在想其他组件可以确定图标,因为它已经确定了您需要哪个。将逻辑放在一个地方。您仍然可以在其他组件中解析 JSON 以获取图标并将其传递给该组件。
标签: reactjs typescript fluentui-react