React Native, from a blank machine to a shipped app · Chapter 4 of 12 · Mobile · new chapter every Monday morning
By the end of this chapter: Lay out any common screen without guessing, and debug a layout that collapsed.
The problem
Marcus is a lead engineer overseeing a team migrating their dashboard from a web app to React Native 0.76. A developer submits a Pull Request for the new "User Profile" screen. On the developer's iPhone 15 Pro Max simulator, it looks perfect.
When Marcus pulls the code to review it on an iPhone SE (3rd Gen) and a budget Android device, the screen is a disaster. On the SE, the "Save Changes" button is pushed off the bottom of the screen, making the form impossible to submit. On the Android device, the profile picture is squashed into an oval because the developer used a fixed width but a percentage-based height.
The team spends three days playing "pixel whack-a-mole," changing a margin here and a padding there, only to find that fixing the layout for one screen size breaks it for another. They miss their sprint goal because they are treating the mobile screen like a static canvas rather than a fluid system. They are guessing how Flexbox works in React Native instead of using the rules of the Yoga layout engine.
Before you start
You must have completed Chapter 2 and 3. You should have a working React Native environment and a basic "Hello World" app running.
Verify your environment by running:
npx react-native info
Ensure the OS version is visible and react-native is version 0.76.0 or higher. If you see command not found, return to Chapter 2.
Why is mobile layout different from the web?
If you have used CSS on the web, you have to unlearn three specific things immediately to be successful with React Native’s implementation of Flexbox, which uses an engine called Yoga.
First, in React Native, every View is display: flex by default. There is no display: block, inline, or grid. If you want to stack elements, you are using Flexbox.
Second, the default flexDirection is column, not row. On a website, elements naturally want to sit side-by-side; on a phone, they naturally want to stack vertically. This matches the way we hold our devices.
Third, there is no "document flow" that allows elements to push the height of the screen indefinitely unless you wrap them in a ScrollView. A View with flex: 1 will expand to fill its parent, but if the parent has no defined height, the child will collapse to 0 pixels. This is the most common reason for a "disappearing" UI.
How do I distribute space?
The core of your layout logic relies on three properties: flex, justifyContent, and alignItems.
The flex property is a ratio. If one child has flex: 1 and another has flex: 2, the second child will take up two-thirds of the available space. If you give a component flex: 1, you are telling it: "Take up all the space your siblings didn't claim."
justifyContent aligns children along the primary axis. Since the default direction is column, justifyContent usually controls vertical alignment (top, center, bottom).
alignItems aligns children along the cross axis. In a default column layout, this controls horizontal alignment (left, center, right).
In React Native 0.71 and later (including our 0.76 setup), we use the gap property to create space between elements. This replaces the old, brittle method of adding marginBottom to every item except the last one.
How do I handle overlapping elements?
Sometimes you need an element to sit on top of everything else—like a "Close" button on a modal or a notification badge.
React Native uses position: 'absolute' for this. Unlike the web, where an absolute element relates to its nearest "positioned" ancestor, in React Native, an absolute element is positioned relative to its immediate parent.
When you use position: 'absolute', the element is pulled out of the flex layout. It no longer takes up space, and other elements will move as if it isn't there. You then use top, right, bottom, and left to pin it.
Build it
We will build a responsive Profile Card that works on any screen size. It will feature a header, a content area that expands, and a fixed footer.
1. Create the base structure
Open your App.tsx and replace the content with this structure. We are using SafeAreaView to ensure our content doesn't sit under the iPhone "notch" or the Android status bar.
import React from 'react';
import { StyleSheet, View, Text, SafeAreaView, TouchableOpacity } from 'react-native';
const App = () => {
return (
<SafeAreaView style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}>Profile Settings</Text>
</View>
<View style={styles.content}>
<View style={styles.card}>
<View style={styles.avatar} />
<View style={styles.info}>
<Text style={styles.name}>Jane Doe</Text>
<Text style={styles.role}>Senior Engineer</Text>
</View>
</View>
</View>
<View style={styles.footer}>
<TouchableOpacity style={styles.button}>
<Text style={styles.buttonText}>Save Changes</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5f5f5',
},
header: {
height: 60,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ffffff',
borderBottomWidth: 1,
borderBottomColor: '#dddddd',
},
headerText: {
fontSize: 18,
fontWeight: '600',
},
content: {
flex: 1,
padding: 20,
},
card: {
flexDirection: 'row',
backgroundColor: '#ffffff',
padding: 15,
borderRadius: 12,
alignItems: 'center',
gap: 15,
// iOS Shadow
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
// Android Shadow
elevation: 3,
},
avatar: {
width: 60,
height: 60,
borderRadius: 30,
backgroundColor: '#007AFF',
},
info: {
flex: 1,
},
name: {
fontSize: 18,
fontWeight: 'bold',
},
role: {
color: '#666',
},
footer: {
padding: 20,
backgroundColor: '#ffffff',
borderTopWidth: 1,
borderTopColor: '#dddddd',
},
button: {
backgroundColor: '#007AFF',
padding: 16,
borderRadius: 8,
alignItems: 'center',
},
buttonText: {
color: '#ffffff',
fontWeight: 'bold',
fontSize: 16,
},
});
export default App;
2. Verify the layout
Run the app on your simulator or device:
npx react-native run-ios
# or
npx react-native run-android
3. Test the "Flex" logic
To confirm flex: 1 is working, look at the content style. It has flex: 1. This pushes the footer to the very bottom of the screen because the content view is expanding to fill all available space between the header and the footer.
4. Confirm the Row alignment
Look at the card style. We set flexDirection: 'row' and alignItems: 'center'. This keeps the blue avatar circle and the text vertically centered relative to each other. Change alignItems to 'flex-start' and save; you will see the text jump to the top of the card. Change it back to 'center'.
5. Add an absolute element
Let's add an "Online" badge to the avatar. Update the avatar View in your JSX:
<View style={styles.avatar}>
<View style={styles.badge} />
</View>
Add the badge style to your StyleSheet:
badge: {
position: 'absolute',
right: 0,
bottom: 0,
width: 16,
height: 16,
borderRadius: 8,
backgroundColor: '#4CD964',
borderWidth: 2,
borderColor: '#ffffff',
},
Observe that the green circle sits perfectly on the bottom-right of the blue circle without affecting the layout of the text next to it.
When this breaks
The disappearing component
Symptom: You added a new View or Image, but the screen is blank or the component is invisible.
Cause: The component or one of its parents has flex: 1 but no defined height or constrained container. In Yoga, flex: 1 of "nothing" is zero.
Fix: Give the parent a fixed height or flex: 1, or give the component itself a minHeight or fixed width/height.
Text cutting off or overlapping
Symptom: On smaller screens, a long name or title overlaps another element or disappears with an ellipsis.
Cause: The container has a fixed width, and the Text component doesn't know it should shrink.
Fix: Wrap the text in a View with flex: 1 and ensure the text doesn't have a whiteSpace: nowrap equivalent (which doesn't exist in RN; text wraps by default unless numberOfLines is set).
The "Bottom Button" is under the keyboard
Symptom: When a user taps an input, the keyboard covers the "Save" button.
Cause: Standard View components do not react to the keyboard.
Fix: This requires a KeyboardAvoidingView (which we will cover in the forms chapter), but for now, ensure your main container is wrapped in a ScrollView if the screen has inputs.
What it costs
Flexbox in React Native is highly efficient because the Yoga engine is written in C++ and calculates layouts off the main UI thread. However, there are two costs to watch for.
First, layout nesting depth. Every level of nested View components requires Yoga to perform another pass of calculations. If you nest views 20 levels deep, you will see a measurable delay in screen rendering (often 10-20ms on low-end Android devices), which can lead to "blank" frames during navigation.
Second, Images have no intrinsic size. On the web, a browser knows the dimensions of an image once it starts loading and shifts the layout. In React Native, if you do not provide a width and height to a remote image, it will render at 0x0 pixels. You must commit to dimensions upfront.
In the interview
The Question: "How do you handle responsive layouts in React Native without media queries?"
The Weak Answer: "I use percentages for everything or I use a library that gives me width/height percentages."
Why it's weak: Percentages are difficult to maintain in complex nested layouts. Relying on "screen percentage" libraries often leads to "jumbo" UIs on tablets where buttons become massive because they are 80% of the screen width.
The Strong Answer: A strong candidate explains that they rely on Flexbox ratios (flex: 1) and intrinsic content size. They mention using SafeAreaView to handle device-specific insets (notches). They discuss the trade-off between fixed dimensions for elements like avatars and fluid dimensions for containers.
Seniority leap: A senior engineer will discuss breakpoints using the useWindowDimensions hook for drastic layout changes (e.g., switching from a list to a grid on tablets) and the performance implications of deep component trees on the Yoga engine. They might mention that onLayout can be used to measure elements dynamically, but warn that it triggers an extra re-render.
Your tasks
- The Spacer: Remove the
gap: 15from thecardstyle. Use aViewwith a fixed width between theavatarand theinfoview to create the same space. This is how we did it beforegapwas supported. - The Proportional Layout: Create a new screen with three boxes. Box A should take up 25% of the height, Box B 50%, and Box C 25%. Use only the
flexproperty (ratios), no percentages or fixed heights. - The Breakage: Take the Profile Card we built. Remove
flex: 1from thecontainerstyle. Observe what happens to the background colour and the footer. Now, add it back but removeflex: 1from thecontentstyle. Note how the footer moves. - The Centering Challenge: Create a square
View(100x100). Inside it, place a smaller square (20x20). Use Flexbox properties on the parent to keep the small square exactly in the dead centre, even if you change the parent's size to 200x200.
Your tasks this week
Do the exercises above before the next chapter. Reading a tutorial and doing
one are different activities and only one of them changes what you can build.
Stuck on any of them? Say so — describe what you tried and what happened:
tell me where you got stuck. I read every one, and the questions
that come back more than twice get answered in the next chapter.
React Native, from a blank machine to a shipped app
Chapter 4 of 12. New chapter every Monday morning.
Next: Navigation: stacks, tabs and the back button people press.
· The full syllabus and every chapter so far
· Subscribers also get the condensed notes for this chapter, the running
recap of everything the series has covered, and the extended guidance:
subscribe
Written by Amit Chakraborty — founding engineer and senior architect: React Native, AI and RAG systems, production architecture. Portfolio · LinkedIn · GitHub.
Need this built, reviewed or taught to your team? Get in touch or email amit@devamit.co.in. Available for senior and founding engineering roles, consulting and training, remote worldwide.
Top comments (0)