[英]Pass array of strings to React component with string coming from json file and allow inline span tags
I'm trying to get an array of strings to display as paragraphs and allow an inline span tag inside of these strings. 我正在尝试获取一个字符串数组以显示为段落,并允许在这些字符串中使用内联span标签。
My issue comes is when the value is added inside the paragraph the decodes the "<" and ">" tag start and ends to their decoded values "<" and ">" 我的问题是,当将值添加到段落中时,将解码“ <”和“>”标记开始,并结束其解码后的值“ <”和“>”
Is there an easy way to get this working without making a specific component for this case? 有没有一种简单的方法就可以在不为此情况创建特定组件的情况下使它正常工作?
My React component is as follows 我的React组件如下
const Paragraphs = ({ data, languageText }) => {
if (data) {
const texts = languageText[data.languageKey];
return (
<section>
{texts && texts.map(text => <p>{text}</p>)}
</section>
);
} else {
console.warn(`webpage::element: 'PARAGRAPHS' lack content`);
}
}
export default Paragraphs;
this is the array that is passed to texts 这是传递给文本的数组
[
"When a Relic is found or assembled from Relic Fragments, a payout is rewarded, based on its rarity. <span style=\"font-weight: bold\">The rarer the item, the higher the payout!</span>",
"In addition to Relics, Gold Coins can also be found."
],
默认情况下,React会转义HTML以防止XSS只使用dangerouslySetInnerHTML
使用SetInnerHTML ,请看官方文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.