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?
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>
来源:https://stackoverflow.com/questions/34231902/is-there-some-way-to-avoid-html-escaping-of-text-children-when-calling-react-cre