Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show html entity using React?

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&sup3;

like image 521
JoeTidee Avatar asked May 22 '17 15:05

JoeTidee


People also ask

How do I show text in HTML React?

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).

HOW include HTML file in React?

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.

Can I use HTML tags 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.

Does React escape 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.


2 Answers

Another option is to use fromCharCode method:

const formatArea = (val) => {     return val + ' ft' + String.fromCharCode(179); } 
like image 66
Avishay28 Avatar answered Sep 22 '22 23:09

Avishay28


New way using React's fragments:

<>&sup3;</> 

In your case it can be:

const formatArea = function(val){     return <>{val + ' '}&sup3;</> } 
like image 33
torvin Avatar answered Sep 19 '22 23:09

torvin