简体   繁体   English

在 React 中危险设置的内部 html 中添加图标

[英]Add Icon in dangerously set Inner html in React

I am trying to add a icon in between string text using dangerously set inner html while div tag is working but arrow icon right (from material ui) is not working it.我试图在 div 标签工作时使用危险设置的内部 html 在字符串文本之间添加一个图标,但右侧箭头图标(来自材料 ui)不起作用。 also noticed that it turns to lower case when actual html is rendered.还注意到,当呈现实际的 html 时,它会变成小写。

here is my code one of the strPath is "World->Americas->Latin America and the Caribbean"这是我的代码,其中 strPath 是“世界->美洲->拉丁美洲和加勒比地区”

let keyArray = Object.keys(selectedRegionsLocal);
    let listUI = [];
    for (let i = 0; i < keyArray.length; i++) {
        let strPath = selectedRegionsLocal[keyArray[i]].path;
        const pieces = strPath.split("->");
        let result = pieces.join(" <ArrowRightIcon></ArrowRightIcon> ")
        let fresult = '<div>' + result + '</div>';
        console.log('result ', fresult);
        listUI.push(
            <Grid dangerouslySetInnerHTML={{ __html: fresult }} />
        )
    }
    return listUI;

this is what i am getting in dev tools -> element section这就是我在开发工具中得到的 -> 元素部分

在此处输入图片说明

image of actual value实际价值的形象

在此处输入图片说明

what can i do to turn the above actual value to this我该怎么做才能把上面的实际值变成这个

在此处输入图片说明

You can simply put an icon on each item:您可以简单地在每个项目上放置一个图标:

Assume that your keyArray is something like this:假设您的keyArray是这样的:

const keyArray = ["World", "Americas", "South", "America"];

Now, you can use map function to add an icon for each item:现在,您可以使用map功能为每个项目添加一个图标:

const result = data.map((item, i) => (
  <div key={i}>
    <ArrowRightIcon />
    <span>{item}</span>
  </div>
));

结果

Now, the result is ready to use, with a parent div and flex style, the final result will be produced:现在, result可以使用了,使用父divflex样式,将生成最终结果:

<div style={{ display: "flex" }}>{result}</div>

最后结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM