Answer the question
In order to leave comments, you need to log in
How to solve the problem with rendering a string in reactjs (backslashes, html, special characters)?
Let's say there is a line in the database (let's say a comment from the user)
ssss\ss<script>alert(123)</script>ss\\ssssss\\\sssss\\\\sssss\\\\\ssss
<div id="content"></div>
<script type="text/javascript">
var comment = '{{ comment|e('html')|e('js') }}';
</script>
class H1 extends React.Component {
render() {
return <div dangerouslySetInnerHTML={{__html: this.props.comment}}></div>;
}
}
ReactDOM.render(
<H1 comment={comment} />,
document.getElementById('content')
);
Answer the question
In order to leave comments, you need to log in
so I seem to be too clever
here is a more beautiful option
<script type="text/javascript">
var comment = '{{ comment|e('js') }}';
</script>
class H1 extends React.Component {
render() {
return <h1>{this.props.comment}</h1>;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question