Home > Net >  How to parse string HTML using javascript?
How to parse string HTML using javascript?

Time:07-10

I have response data in form of:

'<b>How can I waive the underage fee?</b><br>\n You … have marked your age correctly before searching.'

So the question is, how can I use that data in React Component? I have to use it between two divs. Given data is string form of html syntax.

CodePudding user response:

If you want to display this html content in a component, you can use dangerouslySetInnerHTML this way:

<div dangerouslySetInnerHTML={{__html:data}} />

This will inject the HTML in you DOM. As mentioned in the doc, be aware of risks involved with injecting some unknown HMTL.

Source : https://fr.reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml

  • Related