我想在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.

当前回答

在需要换行的地方使用{"\n"}

其他回答

这段代码适用于我的环境。(react-native 0.63.4)

const charChangeLine = `
`
// const charChangeLine = "\n" // or it is ok

const textWithChangeLine = "abc\ndef"

<Text>{textWithChangeLine.replace('¥n', charChangeLine)}</Text>

结果

abc
def
<Text>
Hi~{"\n"}
this is a test message.
</Text>

这样做:

<文本> {“嗨,这是一个测试消息。”} <文本/ >

这应该做到:

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

你可以这样做:

{'创建\ nYour帐户'}