'Is there some way to avoid HTML escaping of text children when calling React.createElement?
Consider the following call to React.createElement
:
React.createElement('span', null, null, ['><',])
This will cause React to escape >
and <
. Is there some way to avoid this text being escaped?
Solution 1:[1]
You can use dangerouslySetInnerHTML
const Component = () => (
<span dangerouslySetInnerHTML={{ __html: '><' }} />
);
ReactDOM.render(
<Component />, document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>
or use Unicode codes
instead of HTML special characters
const Component = () => (
<span>{'\u003E\u003C'}</span>
);
ReactDOM.render(
<Component />, document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |