I am wanting to show the 'cubic' html entity (superscript 3). I am doing like this:
const formatArea = function(val){ return val + " ft³"; }
where formatArea
is being called from inside the component':
render(){ return ( <div> {formatArea(this.props.area)} </div> ); }
but, the browser is showing it as ft³
To render the html string in react, we can use the dangerouslySetInnerHTML attribute which is a react version of dom innerHTML property. The term dangerously is used here to notify you that it will be vulnerable to cross-site scripting attacks (XSS).
If you want to include static html in ReactJS. You need to use html-loader plugin if you are using webpack to serve your react code. That is it. Now you can use static html files to load your html files in react.
By default, React does not permit you to inject HTML in a component, for various reasons including cross-site scripting. However, for some cases like a CMS or WYSIWYG editor, you have to deal with raw HTML.
If you want to display an HTML entity within dynamic content, you will run into double escaping issues as React escapes all the strings you are displaying in order to prevent a wide range of XSS attacks by default. There are various ways to work-around this issue.
Another option is to use fromCharCode method:
const formatArea = (val) => { return val + ' ft' + String.fromCharCode(179); }
New way using React's fragments:
<>³</>
In your case it can be:
const formatArea = function(val){ return <>{val + ' '}³</> }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With