问题
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