Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a line break into a <Text> component in React Native?

I want to insert a new line (like \r\n, <br />) in a Text component in React Native.

If I have:

<text> <br /> Hi~<br /> this is a test message.<br /> </text> 

Then React Native renders Hi~ this is a test message.

Is it possible render text to add a new line like so:

Hi~ this is a test message. 
like image 383
Curtis Avatar asked Sep 09 '15 01:09

Curtis


People also ask

How do I add a line break in text react native?

To insert a line break into a Text component in React Native, we can add the '\n' character string. to add {'\n'} into the content of Text .

How do you put a line through text in React?

Use the textDecoration property to strikethrough text in React, e.g. <span style={{textDecoration: 'line-through'}}> .

How do you break a line in a string React?

In React, you use the self-closing <br> tag to produce a line break between the text or a section, whereas in HTML you'd use <br> … </br> .


2 Answers

This should do it:

<Text> Hi~{"\n"} this is a test message. </Text> 
like image 69
Chris Ghenea Avatar answered Oct 06 '22 00:10

Chris Ghenea


You can also do:

<Text>{` Hi~ this is a test message. `}</Text> 

Easier in my opinion, because you don't have to insert stuff within the string; just wrap it once and it keeps all your line-breaks.

like image 42
Venryx Avatar answered Oct 06 '22 01:10

Venryx