我想在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.
当前回答
Use:
<Text>{`Hi,\nCurtis!`}</Text>
结果:
你好, 柯蒂斯!
其他回答
你可以这样做:
{'创建\ nYour帐户'}
这对我很有效
<Text>{`Hi~\nthis is a test message.`}</Text>
(react-native 0.41.0)
如果有人正在寻找一个解决方案,你想要在数组中的每个字符串有一个新的行,你可以这样做:
import * as React from 'react';
import { Text, View} from 'react-native';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
description: ['Line 1', 'Line 2', 'Line 3'],
};
}
render() {
// Separate each string with a new line
let description = this.state.description.join('\n\n');
let descriptionElement = (
<Text>{description}</Text>
);
return (
<View style={{marginTop: 50}}>
{descriptionElement}
</View>
);
}
}
请参阅小吃现场示例:https://snack.expo.io/@cmacdonnacha/react- nativenew -break-line-example
2021,这适用于REACT状态值(你必须添加空divs,就像返回语句一样)
这一点。setState({form:(<> line 1 <br /> line 2 </>)})
这段代码适用于我的环境。(react-native 0.63.4)
const charChangeLine = `
`
// const charChangeLine = "\n" // or it is ok
const textWithChangeLine = "abc\ndef"
<Text>{textWithChangeLine.replace('¥n', charChangeLine)}</Text>
结果
abc
def