我想在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.
当前回答
在文本和css空格中使用\n: pre-wrap;
其他回答
只需在Text标签中放入{'\n'}
<Text>
Hello {'\n'}
World!
</Text>
可以使用{'\n'}作为换行符。 嗨~ {'\n'}这是一个测试消息。
2021,这适用于REACT状态值(你必须添加空divs,就像返回语句一样)
这一点。setState({form:(<> line 1 <br /> line 2 </>)})
https://stackoverflow.com/a/44845810/10480776 @Edison D'souza的答案正是我一直在寻找的。但是,它只是替换字符串的第一次出现。下面是我处理多个<br/>标签的解决方案:
<Typography style={{ whiteSpace: "pre-line" }}>
{shortDescription.split("<br/>").join("\n")}
</Typography>
抱歉,由于声誉评分限制,我无法评论他的帖子。
在文本和css空格中使用\n: pre-wrap;