Skip to content
DeveloperMemos

Working with Text component in React Native

React Native, Text Component, Mobile Development1 min read

React Native is a popular framework for building native mobile applications on iOS and Android platforms. It allows developers to write code in JavaScript and create native user interfaces that are indistinguishable from those built using Objective-C or Java. One of the key components of any mobile application is the ability to display text, and in this post, we will explore how to use the Text component in React Native.

The Text component is used to display text in a mobile application. It can be styled using CSS-like properties such as font size, color, and alignment. Here's an example of how to use the Text component:

1import React from 'react';
2import { Text, View } from 'react-native';
3
4const App = () => {
5 return (
6 <View>
7 <Text>Hello World!</Text>
8 </View>
9 );
10};
11
12export default App;

In the above example, we import the Text and View components from the react-native library. We then render a View component that contains a Text component with the text "Hello World!".

We can also style the Text component using CSS-like properties. For example, we can change the font size and color of the text:

1import React from 'react';
2import { Text, View } from 'react-native';
3
4const App = () => {
5 return (
6 <View>
7 <Text style={{ fontSize: 24, color: 'red' }}>Hello World!</Text>
8 </View>
9 );
10};
11
12export default App;

In this example, we set the font size to 24 and the color to red.

The Text component also supports text alignment using the textAlign property. Here's an example that aligns the text to the center:

1import React from 'react';
2import { Text, View } from 'react-native';
3
4const App = () => {
5 return (
6 <View>
7 <Text style={{ fontSize: 24, color: 'red', textAlign: 'center' }}>Hello World!</Text>
8 </View>
9 );
10};
11
12export default App;

In addition to these basic properties, the Text component provides many other styling options, such as textDecorationLine for underlining or striking through text, lineHeight for setting the height of a line of text, and fontFamily for specifying the font family.

In closing, the Text component is a fundamental component in React Native for displaying text in mobile applications. It can be styled using CSS-like properties and has many useful attributes for customizing the display of text. With the examples provided in this post, you should have a good understanding of how to use the Text component in your React Native projects!