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

当前回答

你也可以把它作为一个常量添加到你的渲染方法中,这样很容易重用:

  render() {
    const br = `\n`;
     return (
        <Text>Capital Street{br}Cambridge{br}CB11 5XE{br}United Kingdom</Text>
     )  
  }

其他回答

你也可以把它作为一个常量添加到你的渲染方法中,这样很容易重用:

  render() {
    const br = `\n`;
     return (
        <Text>Capital Street{br}Cambridge{br}CB11 5XE{br}United Kingdom</Text>
     )  
  }

这应该做到:

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

可以使用{'\n'}作为换行符。 嗨~ {'\n'}这是一个测试消息。

Use:

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

结果:

你好, 柯蒂斯!

只需在Text标签中放入{'\n'}

<Text>

   Hello {'\n'}

   World!

</Text>