How can I insert a line break into a text componen

2019-01-30 05:59发布

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.

11条回答
smile是对你的礼貌
2楼-- · 2019-01-30 05:59

You can use {'\n'} as line breaks. Hi~ {'\n'} this is a test message.

查看更多
迷人小祖宗
3楼-- · 2019-01-30 05:59

You can use `` like this:

<Text>{`Hi~
this is a test message.`}</Text>
查看更多
Luminary・发光体
4楼-- · 2019-01-30 06:00

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.

查看更多
疯言疯语
5楼-- · 2019-01-30 06:02

Use:

<Text>{`Hi,\nCurtis!`}</Text>

Result:

Hi,

Curtis!

查看更多
▲ chillily
6楼-- · 2019-01-30 06:05

I cannot test it right now but this should do it:

<Text>
Hi~{"\n"}
this is a test message.
</Text>
查看更多
女痞
7楼-- · 2019-01-30 06:06

This worked for me

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

(react-native 0.41.0)

查看更多
登录 后发表回答