我想在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.
当前回答
有时我会这样写:
<Text>
You have {" "}
{remaining}$ {" "}
from{" "}
{total}$
<Text>
(因为我自己看得更清楚)
其他回答
我知道这是相当古老的,但我提出了一个自动断行的解决方案,允许您以通常的方式传递文本(没有诡计)
我创建了以下组件
import React, {} from "react";
import {Text} from "react-native";
function MultiLineText({children, ...otherProps}) {
const splits = children.split("\\n")
console.log(splits);
const items = []
for (let s of splits){
items.push(s)
items.push("\n")
}
return (
<Text {...otherProps}>{items}</Text>
);
}
export default MultiLineText;
然后你就可以这样使用它了。
<MultiLineText style={styles.text}>This is the first line\nThis is teh second line</MultiLineText>
<Text>
Hi~{"\n"}
this is a test message.
</Text>
有时我会这样写:
<Text>
You have {" "}
{remaining}$ {" "}
from{" "}
{total}$
<Text>
(因为我自己看得更清楚)
嘿,把它们这样放,这对我很有用!
< div > <p style={{fontWeight: "bold",空格:"pre-wrap"}}> {"} 你好{" \ n "} < / p > {" \ n "} <p>我在这里</p> < / div >
这样做:
<文本> {“嗨,这是一个测试消息。”} <文本/ >