Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output < inside Text component in React Native?

Like this: <Text> Math inequality: 3 < 2x </Text>

How do I escape such characters so that JSX doesn't think it's a part of the component syntax?

like image 391
Magne Avatar asked Mar 10 '18 12:03

Magne


People also ask

What is TextInput in React Native?

TextInput is a Core Component that allows the user to enter text. It has an onChangeText prop that takes a function to be called every time the text changed, and an onSubmitEditing prop that takes a function to be called when the text is submitted.


2 Answers

After some testing, I found out that @stackdave's answer does not work. I don't know if it works on other versions of RN, but on [email protected], I got the following error: Cannot add a child that doesn't have a YogaNode to a parent without a measure function.

Your best option (safest/better syntax highlighting wise) is to do it like so:

<Text>2 {'>'} 1</Text>
like image 133
Ray Avatar answered Nov 01 '22 20:11

Ray


You could do it easily using the following manner:

let localizableString = 'Math inequality: 3 < 2x' //It is better to separate string literals from code for easier localization.

And then in your Component's file:

<Text {...textProps}>{localizableString}</Text>
like image 21
Omar Salah Elboredy Avatar answered Nov 01 '22 20:11

Omar Salah Elboredy