我正在尝试使用React.Createelement动态创建MUI图标。但是,反应降低了元素。有办法保留案件吗?
我正在从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
update :
如果您使用
https://www.npmjs.com/package/package/@material-ui/icons
(或任何其他库),则可以动态导入所有在这样的组件中:原始答案:
如果API输出是可预测的(
service.icon
只是您定义的组件的一个组件),则可以做到这一点
:随心所欲
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:Original answer:
If the API output is predictable (
service.icon
is just a component of the components you defined)You can do this:
It'll work as you wish
简单的方法要执行此操作,
在您的
.jsx
文件中:作为先决条件,您必须在yout
index.html
voilà中包含此行!
来源:
Simple way to do this,
In your
.jsx
file :As a prerequisite, you must include this line in yout
index.html
Voilà !
Sources :
docs 指定:
您正在尝试使用字符串
“ AdminPanelSettingsicon”
创建一个元素。取而代之的是,您应该通过上课来尝试此操作:您可以通过将列表转换为使用类作为值来做到这一点:
The docs specify:
You're attempting to create an element using the string
"AdminPanelSettingsIcon"
. Instead you should be attempting this by passing in the class:You can do this by converting your list to use classes as the value: