Smaïne
Smaïne

Reputation: 1389

Concatenate unicode and variable

I'm newbie in React and I have some issue to display dynamic unicode value ?

{'\u{1F680}'} become {'\u{MyVar}'}

Upvotes: 8

Views: 1138

Answers (1)

Treycos
Treycos

Reputation: 7492

String.fromCodePoint will get you the character from its numeric code point, and parseInt will get you the number from a hex string.

Your conversion will look like the following : String.fromCodePoint(parseInt(MyVariable, 16))

Working example :

const App = ({ unicode }) => <p> 3, 2, 1, GO ! {String.fromCodePoint(parseInt(unicode, 16))}</p>

ReactDOM.render(<App unicode='1F680'/>, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.5.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.5.2/umd/react-dom.production.min.js"></script>
<div id='root'>

Upvotes: 7

Related Questions