Can I sanitize string without using dangerouslySetInnerHTML in React JSX. Requirement is to check the length of sanitized html string and then only include the component. Something like
var SomeComponent = React.createClass({
render:function(){
return (
{this.props.htmlString.length > 0 ? <Child htmlString={this.props.htmlString} : null}
)
}
})
var Child = React.createClass({
render:function(){
return (
<p dangerouslySetInnerHTML={{__html: this.props.htmlString}}></p>
)
}
})
Everything works fine but fails when this.props.htmlString=' ' . In this case length > 0 and component gets included. So I want to check the length of innerHTML before the element inclusion itself.
One Possible Solution I came across is something like :
var html = " ";
var div = document.createElement('div');
div.innerHTML = html; //check the length of innerHTML now
But I am looking for a cleaner one more of react type.