Pass array of strings to React component with string coming from json file and allow inline span tags

社会主义新天地 提交于 2019-12-11 16:55:53

问题


I'm trying to get an array of strings to display as paragraphs and allow an inline span tag inside of these strings.

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

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."
],

回答1:


By default, React escapes HTML to prevent XSS just use dangerouslySetInnerHTML, take a look at oficial docs



来源:https://stackoverflow.com/questions/56283506/pass-array-of-strings-to-react-component-with-string-coming-from-json-file-and-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!