How to insert a line break into a Text component in React Native?
To insert a line break into a text component in react native we can add the {'\n'}
character string or add the next line in the string literal.
we will see some example code for the above two cases and if you are calling the data via API you can add the string replace function as explained below.
First, we will see some examples for adding in the {'\n'}
in the Text Component after \n the text will be shown in the next line.
Instead of that, we can enter the content next line which we want to show in the next line like the below example.
import * as React from 'react';
import { View, StyleSheet, Text } from 'react-native';
export default class MyComponent extends React.Component {
render() {
return (
<View>
<Text>
First Line {'\n'} Second Line.
</Text>
</View>
);
}
}
or we can use the string literal directly in the Text Component text like the below example.
import * as React from 'react';
import { View, StyleSheet, Text } from 'react-native';
export default class MyComponent extends React.Component {
render() {
return (
<View>
<Text>
`First Line
Second Line`.
</Text>
</View>
);
}
}
Insert a line break into a Text component From the API Data.
sometimes we will get the data directly via API for that case to show the content in the next line in the side text component we can use the below example.
let textValue = stringToReplace.replace(/\\n/g,'\n');
Also, enter the auto line break based on the specific width you can use the below code here instead of maxwidth we can specify device width also.
<View>
<Text style={{ maxWidth:200 }}>
Enter the text in nextline in react native sample text,
which may have more than three lines
</Text>
</View>
Conclusion on to insert the line break or text in the next line React Native.
To insert a line break or text in the next line into a Text component in React Native, we can add the ‘\n’ character string. Please let us know if you have any queries or clarifications in the comments.
Also, please check it out tutorial for how to hide the keyboard in react native.
Leave a Reply