Concatenate unicode and variable

前端 未结 1 1504
滥情空心
滥情空心 2021-01-06 01:43

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

{\'\\u{1F680}\'} become {\'\\u{MyVar}\'}
相关标签:
1条回答
  • 2021-01-06 02:35

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

    0 讨论(0)
提交回复
热议问题