我想在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>
(因为我自己看得更清楚)
其他回答
我使用p标签的新行。这里我粘贴了代码,这对大家都有帮助。
const new2DArr = associativeArr.map((crntVal )=>{
return <p > Id : {crntVal.id} City Name : {crntVal.cityName} </p>;
});
你还可以:
<Text>{`
Hi~
this is a test message.
`}</Text>
在我看来更简单,因为你不需要在字符串中插入东西;只要换行一次,就能保留所有换行符。
这应该做到:
<Text>
Hi~{"\n"}
this is a test message.
</Text>
这应该可以做到!
<Text style={{styles.text}}>{`Hi~\nthis is a test message.`}</Text>
最干净和最灵活的方法之一是使用模板字面量。
使用它的一个优点是,如果你想在文本体中显示字符串变量的内容,它更干净和直接。
(请注意使用反引号)
const customMessage = 'This is a test message';
<Text>
{`
Hi~
${customMessage}
`}
</Text>
会导致
Hi~
This is a test message