I have span –
<span> {textToRender} </span>
I am rendering html text (as string) with span.
some text <br/> some text <br/> some text <ol>text
This is getting rendered as text only in javascript. Html tags are not getting applied over it.
<span> {textToRender} </span>
4
Answers
This is expected, as
React.js
automatically escapes the extrapolations to prevent injection attacks and other vulnerabilities. In case you want HTML to get parsed, and proper tags be applied, you can use JSX instead ofString
or set HTML explicitly as follows:Or
convert your
textToRender
to JSX.same as the guys suggest you can use the dangerouslySetInnerHTML,
Although the name suggests danger in dangerouslySetInnerHTML and its use, taking the necessary measure by using a well-developed sanitizer ensures the code to be clean and does not run unexpected scripts when rendered within the React node.
you can use DOMPurify for exemple
Please try it following way:
I hope it helps you.