-1

Need

const stringWithBr = 'aaaaaaa<br />bbbbbbb';

ReactDOM.render(
  <div>{stringWithBr}</div>,
  document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

help, why this example not working? in fact i need get:

aaaaaaa
bbbbbbb
  • React escapes the contents of the string you replace for security reason. If `stringWithBr` was a user provided string you do **not** want it to be evaluated as HTML. You want to use [`dangerouslySetInnerHTML`](https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml) – GACy20 Feb 21 '22 at 13:53

1 Answers1

0

This is a feature of React against XSS.

It provides dangerouslySetInnerHTML as an escape hatch and it's documented here.

If possible, please avoid using this, though, especially if the input can be injected by the user somehow.

Juho Vepsäläinen
  • 26,573
  • 12
  • 79
  • 105