我正在尝试使用React.Createelement动态创建MUI图标。但是,反应降低了元素。有办法保留案件吗?

发布于 2025-01-28 06:12:03 字数 598 浏览 6 评论 0原文

我正在从JSON获得Service.icon,所以看起来

[
  {
    "icon": "AdminPanelSettingsIcon",
  }
]

我正在使用React.Createlement(),就像这样,

data.map((service) => {
  let icon = React.createElement(
    service.icon, 
    { key: service.icon }, 
    null);
});

我的输出是< adminpanelsettingsicon>>/adminpanelsettingsicon>

无论如何都在那里保留情况,或转换为pascalcase,以便呈现So < adminpanelsettingsicon><</adminpanelsettingsicon>

注意:无论如何,无论如何,无论如何,无论如何都可以动态地显示基于指定的json aka api api api api呼叫。

I am getting service.icon from JSON, so it looks like so

[
  {
    "icon": "AdminPanelSettingsIcon",
  }
]

I am using React.createElement() like so,

data.map((service) => {
  let icon = React.createElement(
    service.icon, 
    { key: service.icon }, 
    null);
});

my output is <adminpanelsettingsicon></adminpanelsettingsicon>

is there anyway to keep case, or convert to PascalCase so it renders like so <AdminPanelSettingsIcon></AdminPanelSettingsIcon>

Note: Is there anyway to dynamically display mui icons based on specified JSON aka api call.

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

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

发布评论

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

评论(3

够运 2025-02-04 06:12:04

update

如果您使用https://www.npmjs.com/package/package/@material-ui/icons(或任何其他库),则可以动态导入所有在这样的组件中:

//Preparing, it should run inside async function
let maps = {}
let listIcon = [];
await import('@material-ui/icons').then(res => {
      listIcon = res
})

for(let icon of listIcon){
    maps[icon.constructor.name.toLowerCase()] = icon;
}

//Render
data.map((service) => {
    const component = maps[service.icon] || DefaultComponent;
    let icon = React.createElement(
        component,
        { key: component },
        null);
});

原始答案

如果API输出是可预测的(service.icon只是您定义的组件的一个组件),

则可以做到这一点

//Import...

//Defined maps
const maps = {
    "adminpanelsettingsicon": AdminPanelSettingsIcon,
    "adminpanelsettingsicon2": AdminPanelSettingsIcon2,
    "adminpanelsettingsicon3": AdminPanelSettingsIcon3,
}

//Render
data.map((service) => {
    const component = maps[service.icon] || DefaultComponent;
    let icon = React.createElement(
        component,
        { key: component },
        null);
});

:随心所欲

Update:

If you are using https://www.npmjs.com/package/@material-ui/icons (or any other library), you can dynamically import all of the components like this:

//Preparing, it should run inside async function
let maps = {}
let listIcon = [];
await import('@material-ui/icons').then(res => {
      listIcon = res
})

for(let icon of listIcon){
    maps[icon.constructor.name.toLowerCase()] = icon;
}

//Render
data.map((service) => {
    const component = maps[service.icon] || DefaultComponent;
    let icon = React.createElement(
        component,
        { key: component },
        null);
});

Original answer:

If the API output is predictable (service.icon is just a component of the components you defined)

You can do this:

//Import...

//Defined maps
const maps = {
    "adminpanelsettingsicon": AdminPanelSettingsIcon,
    "adminpanelsettingsicon2": AdminPanelSettingsIcon2,
    "adminpanelsettingsicon3": AdminPanelSettingsIcon3,
}

//Render
data.map((service) => {
    const component = maps[service.icon] || DefaultComponent;
    let icon = React.createElement(
        component,
        { key: component },
        null);
});

It'll work as you wish

夜司空 2025-02-04 06:12:04

简单的方法要执行此操作,

在您的.jsx文件中:

import Icon from '@mui/material/Icon';

export default function MyComponent() {
  const iconName = `home`
  return (<Icon>{iconName}</Icon>);
}

作为先决条件,您必须在yout index.html

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

voilà中包含此行!

来源:

Simple way to do this,

In your .jsx file :

import Icon from '@mui/material/Icon';

export default function MyComponent() {
  const iconName = `home`
  return (<Icon>{iconName}</Icon>);
}

As a prerequisite, you must include this line in yout index.html

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

Voilà !

Sources :

野却迷人 2025-02-04 06:12:03

docs 指定:

React.createElement(
  type,
  [props],
  [...children]
)

类型参数可以是标签名称字符串(例如'div'或
“跨度”),一种反应组件类型(类或函数)或React
片段类型。

您正在尝试使用字符串“ AdminPanelSettingsicon”创建一个元素。取而代之的是,您应该通过上课来尝试此操作:

React.createElement(AdminPanelSettingsIcon)

您可以通过将列表转换为使用类作为值来做到这一点:

[
  {
    // The value is a class now, not a string
    "icon": AdminPanelSettingsIcon,
  }
]

The docs specify:

React.createElement(
  type,
  [props],
  [...children]
)

The type argument can be either a tag name string (such as 'div' or
'span'), a React component type (a class or a function), or a React
fragment type.

You're attempting to create an element using the string "AdminPanelSettingsIcon". Instead you should be attempting this by passing in the class:

React.createElement(AdminPanelSettingsIcon)

You can do this by converting your list to use classes as the value:

[
  {
    // The value is a class now, not a string
    "icon": AdminPanelSettingsIcon,
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文