15

I have the ascii/html code of a checkmark: ✔

✔

In react if I go:

<div>&#10004;</div>

then it works. but if I go

var str = '&#10004;';
<div>{str}</div>

it doesn't. It shows up as &#10004;

any ideas?

class Hello extends React.Component {
  render() {
    var checkmark = '&#10004;';
  
    return <div>Hello {checkmark}</div>;
  }
}

ReactDOM.render(
  <Hello />,
  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"/>
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • worst case, you can use [dangerouslySetInnerHTML](https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml) – Ted Jan 04 '18 at 21:53
  • @foreyez That's what the "Copy snippet to answer" button is for. – Jordan Running Jan 04 '18 at 22:06
  • 1
    If you don't mind using unicode: `var checkmark = '\u2714';` – kuujinbo Jan 04 '18 at 22:15
  • Check the answer of @Mhmdrz_A given for question: https://stackoverflow.com/questions/61347372/adding-a-title-attribute-line-break-using-10-to-a-jsx-element – Germán Mendoza Oct 13 '21 at 16:09
  • Check the answer given by @Mhmdrz_A in https://stackoverflow.com/questions/61347372/adding-a-title-attribute-line-break-using-10-to-a-jsx-element – Germán Mendoza Oct 13 '21 at 16:11
  • Check the answer given by @Mhmdrz_A in https://stackoverflow.com/questions/61347372/adding-a-title-attribute-line-break-using-10-to-a-jsx-element – Germán Mendoza Oct 13 '21 at 16:13

5 Answers5

9

It's pretty easy to see what's going on here by looking at what actually gets rendered:

const checkmark = '&#100004;';

ReactDOM.render(<div id="x">{checkmark}</div>,
  document.getElementById('container'));

console.log(document.getElementById('x').innerHTML);
<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"/>

As you can see, React escapes its output, replacing & with &amp;, just as expected.

The easiest, and correct, solution is to dump the HTML entity and just use the character directly:

class Hello extends React.Component {
  render() {
    var checkmark = '✔';
    return <div>Hello {checkmark}</div>;
  }
}

ReactDOM.render(<Hello />, 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"/>

As long as your page's encoding is declared correctly as UTF-8, this will work in all browsers.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
4

https://jsfiddle.net/hLy9xjw1/

Here is a fiddle with two solutions. One is to use the unicode character instead of the ASCII code. Another is to insert the character directly, but make sure your encoding is correct if you want to use the second option. You can also see that they render slightly differently, in Chrome at least.

<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"/>

class Hello extends React.Component {
  render() {
    return (
      <div>
        <Solution1 />
        <Solution2 />
      </div>
        )
  }
}

class Solution1 extends React.Component {
  render() {
      var checkmark = '\u2714';  
    return <div>Hello {checkmark}</div>;
    }
}

class Solution2 extends React.Component {
  render() {
      var checkmark =  '✓';  
    return <div>Hello {checkmark}</div>;
    }
}
ReactDOM.render(
  <Hello />,
  document.getElementById('container')
);
ecg8
  • 1,362
  • 1
  • 12
  • 16
  • 2
    They render slightly differently because you're using two different characters. `✓` is [U+2713 CHECK MARK](https://www.fileformat.info/info/unicode/char/2713/index.htm); `✔` is [U+2714 HEAVY CHECK MARK](https://www.fileformat.info/info/unicode/char/2714/index.htm). – Jordan Running Jan 04 '18 at 22:19
  • Ah, makes sense. – ecg8 Jan 04 '18 at 22:22
2
<span
 dangerouslySetInnerHTML={{ __html: code }}
/>
BaHaa Jr.
  • 534
  • 1
  • 7
  • 19
0

Simplest implementation is to skip the variable entirely:

...
return (
  <Whatever>{'\u2714'}</Whatever>
);
...

...unless you need it to be a variable for whatever reason :)

Gifford N.
  • 412
  • 5
  • 13
0

Initialize DOM parser


    const parser = new DOMParser();

and use like this to get strings with markups converted to readable text


    label = parser.parseFromString(label,'text/html').body.textContent;