I cant figure out how to correctly add a unicode escape to a dynamic string value in order to render emojis in React.
In my database I am storing the hexcode for that emoji (1f44d)
I've created a styled-component with the correct css to render an emoji
const EmojiWrapper = styled.span(({ label }) =>
css({
role: 'img',
ariaLabel: label || '',
ariaHidden: label ? 'false' : 'true',
})
);
and the usage where symbol is equal to 1f44d just displays '\u{1f44d}'
<EmojiWrapper className="emoji" label={label}>
{`'\\u{${symbol}}'`}
</EmojiWrapper>
I've tried storing the hexcode as an escaped string '\u{1f44d}' but that still displays '\u{1f44d}' as opposed to the emoji.
If I hardcode the hexcode with the escape '\u{1f44d}' the emoji renders correctly:
<EmojiWrapper className="emoji" label={label}>
{'\u{1f44d}'}
</EmojiWrapper>
As does
{eval("'\\u" + {symbol} + "'")}
I am trying to avoid using eval however as I've read it is a bad idea. Any ideas would be much appreciated.