Working with Text, Images, and Buttons
# CHAPTER 11
Working with Text, Images, and Buttons
1. Introduction
A beautifully structured flexbox layout is meaningless if there is no content inside it. In this chapter, we transition from structure to content. We will master Working with Text, Images, and Buttons. These three Core Components represent 95% of the visual elements in any modern application. You will learn how to properly format nested typography, safely load both local and network images, and build highly customizable, touch-responsive buttons usingTouchableOpacity and Pressable.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use the
<Text>component and understand its nesting behaviors.
-
Load local assets and external URLs using the
<Image>component.
-
Understand why the default
<Button>component is rarely used by professionals.
-
Build custom, animated buttons using
<TouchableOpacity>.
- Add vector icons to your application using Expo Icons.
3. The <Text> Component
As discussed, you cannot render strings inside a <View>. They must be inside a <Text> component.
Unlike <View>, <Text> components *do* support inheritance, but only for other <Text> components!
4. The <Image> Component
Mobile apps display two types of images: Local (stored inside the app file size) and Network (downloaded from the internet).
Local Images (Using require)
Used for logos, icons, and static assets. They are bundled instantly.
Network Images (Using uri)
Used for user avatars or dynamic feed photos. Critical Rule: Network images will NOT render unless you provide a physical width and height in the style! React Native needs to know how much space to reserve while the image downloads.
5. Why Not Use <Button>?
React Native provides a built-in <Button title="Click Me" onPress={doSomething} />.
Do not use it for major UI design.
The default <Button> is just a wrapper for the raw iOS and Android system buttons. It looks completely different on an iPhone versus an Android, and you cannot easily change its background color, padding, or text styling.
6. The Solution: <TouchableOpacity>
To create beautiful, cross-platform buttons, professionals use TouchableOpacity.
It is basically a <View> that listens for clicks. When the user taps it, the entire component briefly fades out (dims the opacity), providing excellent visual feedback.
*(Note: React Native also offers <Pressable>, which is a newer, more advanced version of TouchableOpacity that gives you granular control over hit-slop areas and custom press states).*
7. Adding Icons (Expo Vector Icons)
If you are using Expo, you have instant access to thousands of free SVG icons (Material Icons, FontAwesome, Ionicons) without installing any third-party libraries!8. Mini Project: The Profile Header
Objective: Combine Text, Image, and TouchableOpacity into a cohesive unit.9. Common Mistakes
-
Invisible Network Images: You paste a valid URL into an
<Image source={{uri: '...'}} />tag, but the screen is totally blank. Why? Because you forgot to addstyle={{ width: 100, height: 100 }}. Network images have0x0dimensions by default.
10. Best Practices
-
Image Caching: Loading heavy 4K network images on every screen render will lag the app. For production apps, use libraries like
expo-imageorreact-native-fast-image. They cache the image heavily to the phone's local storage so it loads instantly the next time the app is opened.
11. Practice Exercises
-
1.
Write the code to display a local image named
background.jpglocated in your assets folder.
-
2.
Why is
<TouchableOpacity>preferred over the standard<Button>component when building a custom-designed signup screen?
12. MCQs with Answers
A developer places an <Image> component in their app fetching a user's avatar from a remote URL. The URL is correct, but the image is completely invisible. What is the most likely cause?
When styling nested typography, what unique behavior does the <Text> component exhibit that the <View> component does not?
13. Interview Questions
-
Q: Explain the syntax difference in the
sourceprop when loading a local asset via the bundler versus loading a remote network asset. Why does this syntax difference exist?
-
Q: Contrast
<TouchableOpacity>with<Pressable>. When would an architecture team strictly mandate the use of<Pressable>over the older Touchable components?
- Q: How do you handle different image pixel densities (e.g., Retina displays, 2x, 3x) for local static assets in a React Native project to ensure they do not appear blurry on high-end devices?
14. FAQs
Q: My text is cutting off at the end of the screen instead of wrapping. How do I fix it? A: Ensure the parent<View> has flex: 1 or flexShrink: 1 applied. If you want the text to cleanly end with dots (e.g., "This is a long..."), use the numberOfLines={1} prop on the <Text> component!
15. Summary
In Chapter 11, we populated our structures with rich content. We mastered the<Text> component, utilizing nested inheritance for complex typography formatting. We successfully loaded both local and dynamic remote assets using the <Image> component, acknowledging the critical requirement of explicit dimensions for network fetches. Finally, we bypassed the rigid native <Button> limitations by constructing our own highly customizable, interactive elements using <TouchableOpacity>.