我想在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.
我想在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”就可以了
<Text>
First Line {'\n'} Second Line.
</Text>
方法2:在字符串字面量中添加换行符,如下所示。
<Text>
`First Line
Second Line`.
</Text>
要了解更多信息,请参考下面的教程。
https://sourcefreeze.com/how-to-insert-a-line-break-into-a-text-component-in-react-native/
其他回答
我需要一个在三元运算符中分支的单行解决方案,以保持我的代码很好地缩进。
{foo ? `First line of text\nSecond line of text` : `Single line of text`}
Sublime语法高亮显示有助于突出显示换行字符:
简单使用反勾号(es6特性)
解决方案1
const Message = 'This is a message';
<Text>
{`
Hi~
${Message}
`}
</Text>
解决方案2在文本中添加“\n”
<Text>
Hi~{"\n"}
This is a message.
</Text>
这是一个很好的问题,你可以用多种方法来做这个问题 第一个
<View>
<Text>
Hi this is first line {\n} hi this is second line
</Text>
</View>
这意味着您可以使用{\n}反斜杠n来断行
第二个
<View>
<Text>
Hi this is first line
</Text>
<View>
<Text>
hi this is second line
</Text>
</View>
</View>
这意味着你可以使用另一个<查看>组件里面首先<查看>和包装它周围<文本>组件
快乐的编码
Use:
<Text>{`Hi,\nCurtis!`}</Text>
结果:
你好, 柯蒂斯!
嘿,把它们这样放,这对我很有用!
< div > <p style={{fontWeight: "bold",空格:"pre-wrap"}}> {"} 你好{" \ n "} < / p > {" \ n "} <p>我在这里</p> < / div >