Ascii/HTML character code doesn't work within React variable

11,526

Solution 1

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

const checkmark = '𘚤';

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.

Solution 2

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')
);

Solution 3

<span
 dangerouslySetInnerHTML={{ __html: code }}
/>
Share:
11,526

Related videos on Youtube

Shai UI
Author by

Shai UI

front-end developer in finance. especially html5/javascript/css based apps for mobile/desktop/tablets, node.js on the back-end. my main interests are heavy GUI, 2d/3d, data visualizations. check out my youtube channel: https://www.youtube.com/channel/UCJagBFh6ClHpZ2_EI5a3WlQ

Updated on May 22, 2022

Comments

  • Shai UI
    Shai UI almost 2 years

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

    &#10004;
    

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

    • kuujinbo
      kuujinbo over 6 years
      If you don't mind using unicode: var checkmark = '\u2714';
  • Jordan Running
    Jordan Running over 6 years
    They render slightly differently because you're using two different characters. is U+2713 CHECK MARK; is U+2714 HEAVY CHECK MARK.
  • ecg8
    ecg8 over 6 years
    Ah, makes sense.