我想在React Native的文本组件中插入一个新行(如\r\n, <br />)。

如果我有:

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

然后React Native渲染Hi~这是一个测试消息。

它是可能的渲染文本添加一个新的行像这样:

Hi~
this is a test message.

当前回答

这应该可以做到!

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

其他回答

这对我很有效

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

(react-native 0.41.0)

简单使用反勾号(es6特性)

解决方案1

const Message = 'This is a message';

<Text>
{`
  Hi~
  ${Message}
`}
</Text>

解决方案2在文本中添加“\n”

<Text>
Hi~{"\n"}
This is a message.
</Text>

这应该可以做到!

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

有时我会这样写:

<Text>
  You have {" "}
  {remaining}$ {" "}
  from{" "}
  {total}$
<Text>

(因为我自己看得更清楚)

你可以这样做:

{'创建\ nYour帐户'}