SoFunction
Updated on 2025-04-11

react how to escape string into html statement

Escape strings into html statement

When using reactjs library, you will encounter a string that will insert a piece of html and then insert it into the page to display it in the form of html.

However, if you insert it directly, the page displays this string without escaping. You can insert it in the following way, and it can be displayed in the form of html:

&lt;div dangerouslySetInnerHTML={{__html: "<p>Love the death and pay attention to the government</p>"}} /&gt;

Escape tagged strings to html

use

dangerouslySetInnerHTML={{ __html: htmlString}}

example

return (
      <div>{(value =>
        (
          <div className={}>
            <p dangerouslySetInnerHTML={{ __html:  }}  />
          </div>
        )
      )}
      </div>
    )

The above is personal experience. I hope you can give you a reference and I hope you can support me more.