Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在React中按名称渲染动态组件_Javascript_Html_Reactjs_Jsx - Fatal编程技术网

Javascript 在React中按名称渲染动态组件

Javascript 在React中按名称渲染动态组件,javascript,html,reactjs,jsx,Javascript,Html,Reactjs,Jsx,如何在React中设置自定义标记 每个导入都是一个JSX元素。例如,GitHub是从如下所示的文件导入的: 从“React”导入React; 常量GitHub=()=>( GitHub图标 ); 导出默认GitHub; 其他图标以相同的格式从各自的文件中导入。现在我想循环查看数据,以渲染每个图标 import React,{Component}来自'React'; 从“/../assets/GitHub”导入GitHub 从“/../assets/LinkedIn”导入LinkedIn 从“/

如何在React中设置自定义标记

每个导入都是一个JSX元素。例如,GitHub是从如下所示的文件导入的:

从“React”导入React;
常量GitHub=()=>(
GitHub图标
);
导出默认GitHub;
其他图标以相同的格式从各自的文件中导入。现在我想循环查看
数据
,以渲染每个图标

import React,{Component}来自'React';
从“/../assets/GitHub”导入GitHub
从“/../assets/LinkedIn”导入LinkedIn
从“/../assets/Email”导入电子邮件
常数数据=[
{
href:“…”,
咏叹调:“…”,
图标:GitHub,
标签:“…”,
},
{
href:“…”,
咏叹调:“…”,
图标:LinkedIn,
标签:“…”,
},
{
href:“…”,
咏叹调:“…”,
图标:电子邮件,
标签:“…”,
}
];
常量按钮=({href,aria,icon,label})=>{
返回(
);
};
类按钮扩展组件{
render(){
返回(
{DATA.map((props,i)=>(
))}
);
}
}
导出默认按钮;
代码行是我遇到困难的地方是
。我不知道如何在循环时设置自定义HTML标记

此外,任何其他反馈都将不胜感激。在
DATA
中,我应该将每个图标存储为导入的别名,还是应该使用
图标:“GitHub”


谢谢

您需要将其重命名为
图标
,并使用大写字母
I
(此约定类似于
),但除此之外,您可以像使用任何其他组件一样使用它:

const Button = ({ href, aria, icon: Icon, label }) => {
    return (
        <span className="button-container">
            <a className="button" href={href} target="_self" aria-label={aria} rel="noopener noreferrer">
                <Icon className="icon"/>
                <span className="icon_title">{label}</span>
            </a>
        </span>
    );
};
const按钮=({href,aria,icon:icon,label})=>{
返回(
);
};

您需要将其重命名为
图标
,并使用大写字母
I
(此约定类似于
),但除此之外,您使用它就像使用任何其他组件一样:

const Button = ({ href, aria, icon: Icon, label }) => {
    return (
        <span className="button-container">
            <a className="button" href={href} target="_self" aria-label={aria} rel="noopener noreferrer">
                <Icon className="icon"/>
                <span className="icon_title">{label}</span>
            </a>
        </span>
    );
};
const按钮=({href,aria,icon:icon,label})=>{
返回(
);
};

由于React exige大写的组件名称,您可以将该属性分配给名为
Icon
的局部变量,然后将其作为JSX使用:


const Button = ({ href, aria, icon, label }) => {
   let Icon=icon;
    return (
        <span className="button-container">
            <a className="button" href={href} target="_self" aria-label={aria} rel="noopener noreferrer">
                <Icon className="icon"/>
                <span className="icon_title">{label}</span>
            </a>
        </span>
    );
};

常量按钮=({href,aria,icon,label})=>{
让图标=图标;
返回(
);
};
或者将图标设置为数据数组中的元素,如:

const DATA = [
    {
        href: "...",
        aria: "...",
        icon: <GitHub className="icon" />,
        label: "...",
    },
   ...
const数据=[
{
href:“…”,
咏叹调:“…”,
图标:,
标签:“…”,
},
...
然后按如下方式渲染:

const Button = ({ href, aria, icon, label }) => {
    return (
        <span className="button-container">
            <a className="button" href={href} target="_self" aria-label={aria} rel="noopener noreferrer">
                {icon}
                <span className="icon_title">{label}</span>
            </a>
        </span>
    );
};
const按钮=({href,aria,icon,label})=>{
返回(
);
};

请检查此项

因为React exage大写的组件名称,您可以将该属性分配给名为
Icon
的局部变量,然后将其作为JSX使用:


const Button = ({ href, aria, icon, label }) => {
   let Icon=icon;
    return (
        <span className="button-container">
            <a className="button" href={href} target="_self" aria-label={aria} rel="noopener noreferrer">
                <Icon className="icon"/>
                <span className="icon_title">{label}</span>
            </a>
        </span>
    );
};

常量按钮=({href,aria,icon,label})=>{
让图标=图标;
返回(
);
};
或者将图标设置为数据数组中的元素,如:

const DATA = [
    {
        href: "...",
        aria: "...",
        icon: <GitHub className="icon" />,
        label: "...",
    },
   ...
const数据=[
{
href:“…”,
咏叹调:“…”,
图标:,
标签:“…”,
},
...
然后按如下方式渲染:

const Button = ({ href, aria, icon, label }) => {
    return (
        <span className="button-container">
            <a className="button" href={href} target="_self" aria-label={aria} rel="noopener noreferrer">
                {icon}
                <span className="icon_title">{label}</span>
            </a>
        </span>
    );
};
const按钮=({href,aria,icon,label})=>{
返回(
);
};

请检查此

您是否尝试过其他答案?是的,当前正在使用您的解决方案并尝试解决cssi添加了一个运行示例,我检查我的两个解决方案是否有效以及Nicholas Tower的解决方案是否尝试过其他答案?是的,当前正在使用您的解决方案并尝试解决cssi添加了一个运行示例我检查我的两个解决方案和Nicholas Tower的解决方案是否有效