I would like to use this symbol in my React Native project.
I tried using the Unicode encoding like this:
var arrow = "U+0279C";
And in the JSX:
<Text>
{arrow}
</Text>
However, this just displays the encoding literally: U+0279C
.
So any idea how can I use a symbol in JSX?
The curly braces are a special syntax to let the JSX parser know that it needs to interpret the contents in between them as JavaScript instead of a string. You need them when you want to use a JavaScript expression like a variable or a reference inside JSX.
React and React Native use JSX, a syntax that lets you write elements inside JavaScript like so: <Text>Hello, I am your cat!</Text> . The React docs have a comprehensive guide to JSX you can refer to learn even more. Because JSX is JavaScript, you can use variables inside it.
You should use the HTML code for the symbol.
<Text>
➜
</Text>
As described in notes below.. (important, quotes don't seem to work)...
Just to clarify:
<Text>➜</Text>
will work, but<Text>{ '➜' }</Text>
will not.
Use this functions for symbols in this format: & # 1 7 4 ;
/**
* replaces /$#d\+/ symbol with actual symbols in the given string
*
* Returns given string with symbol code replaced with actual symbol
*
* @param {string} name
*/
export function convertSymbolsFromCode(name = '') {
let final = null;
if (name) {
const val = name.match(/&#\d+;/) ? name.match(/&#\d+;/)[0] : false; // need to check whether it is an actual symbol code
if (val) {
const num = val.match(/\d+;/) ? val.match(/\d+;/)[0] : false; // if symbol, then get numeric code
if (num) {
final = num.replace(/;/g, '');
}
}
if (final) {
name = name.replace(/&#\d+;/g, String.fromCharCode(final));
}
}
return name;
}
USAGE
<Text>
{convertSymbolsFromCode(this.state.unicode)}
</Text>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With