Bird
Raised Fist0
React Nativemobile~20 mins

Width, height, and flex in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Box Layout Screen
This screen shows three colored boxes arranged vertically. Each box uses width, height, and flex properties to demonstrate how space is shared and sized in React Native layouts.
Target UI
┌─────────────────────────────┐
│        Box Layout Screen     │
├─────────────────────────────┤
│  ┌───────────────┐          │
│  │   Red Box     │          │
│  │  width: 100   │          │
│  │  height: 100  │          │
│  └───────────────┘          │
│                             │
│  ┌───────────────────────┐  │
│  │     Green Box          │  │
│  │     flex: 1           │  │
│  └───────────────────────┘  │
│                             │
│  ┌────────────────────────────┐
│  │        Blue Box             │
│  │        flex: 2             │
│  └────────────────────────────┘
└─────────────────────────────┘
Create a vertical layout with three boxes stacked top to bottom.
The top box is red with fixed width 100 and height 100.
The middle box is green and uses flex: 1 to fill available space.
The bottom box is blue and uses flex: 2 to fill twice the space of the green box.
Use React Native StyleSheet for styling.
The screen background should be white.
Starter Code
React Native
import React from 'react';
import { View, StyleSheet } from 'react-native';

export default function BoxLayoutScreen() {
  return (
    <View style={styles.container}>
      {/* TODO: Add the three boxes here */}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    flexDirection: 'column',
    // TODO: Add any other container styles if needed
  },
  // TODO: Add styles for redBox, greenBox, blueBox
});
Task 1
Task 2
Solution
React Native
import React from 'react';
import { View, StyleSheet } from 'react-native';

export default function BoxLayoutScreen() {
  return (
    <View style={styles.container}>
      <View style={styles.redBox} />
      <View style={styles.greenBox} />
      <View style={styles.blueBox} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    flexDirection: 'column'
  },
  redBox: {
    width: 100,
    height: 100,
    backgroundColor: 'red'
  },
  greenBox: {
    flex: 1,
    backgroundColor: 'green'
  },
  blueBox: {
    flex: 2,
    backgroundColor: 'blue'
  }
});

We created a container View with flex: 1 to fill the screen and a white background. Inside it, we stacked three boxes vertically using flexDirection: 'column'.

The red box has fixed width and height of 100, so it stays small at the top.

The green and blue boxes use flex to share the remaining space. The green box uses flex: 1, and the blue box uses flex: 2, so the blue box is twice as tall as the green box.

This shows how fixed sizes and flex work together to control layout in React Native.

Final Result
Completed Screen
┌─────────────────────────────┐
│        Box Layout Screen     │
├─────────────────────────────┤
│  ┌───────────────┐          │
│  │               │          │
│  │    RED BOX    │          │
│  │   100x100 px  │          │
│  └───────────────┘          │
│                             │
│  ┌───────────────────────┐  │
│  │                       │  │
│  │      GREEN BOX         │  │
│  │      flex: 1          │  │
│  └───────────────────────┘  │
│                             │
│  ┌────────────────────────────┐
│  │                            │
│  │         BLUE BOX            │
│  │         flex: 2            │
│  └────────────────────────────┘
└─────────────────────────────┘
The screen shows three colored boxes stacked vertically.
The red box stays fixed size at top.
The green and blue boxes fill the rest of the screen with blue twice as tall as green.
Stretch Goal
Add a button below the blue box that toggles the visibility of the red box.
💡 Hint
Use React useState hook to track red box visibility and conditionally render the red box.

Practice

(1/5)
1. What does the flex property do in React Native layout?
easy
A. It changes the font size of text.
B. It sets a fixed width in pixels.
C. It makes the component grow or shrink to fill available space.
D. It sets a fixed height in pixels.

Solution

  1. Step 1: Understand the role of flex

    The flex property controls how a component grows or shrinks to fill space in its container.
  2. Step 2: Compare with fixed sizes

    Unlike fixed width or height, flex is flexible and adapts to screen size.
  3. Final Answer:

    It makes the component grow or shrink to fill available space. -> Option C
  4. Quick Check:

    Flex = flexible size [OK]
Hint: Flex means flexible size, not fixed pixels [OK]
Common Mistakes:
  • Confusing flex with fixed width or height
  • Thinking flex changes font size
  • Assuming flex sets exact pixel size
2. Which of the following is the correct way to set a fixed width of 100 pixels in React Native style?
easy
A. { width: 100 }
B. { width: '100' }
C. { width: '100px' }
D. { flex: 100 }

Solution

  1. Step 1: Check value type for width

    In React Native, fixed sizes are numbers without quotes, representing pixels.
  2. Step 2: Identify correct syntax

    Only { width: 100 } uses a number correctly; strings or units like 'px' are invalid.
  3. Final Answer:

    { width: 100 } -> Option A
  4. Quick Check:

    Fixed size = number without quotes [OK]
Hint: Use numbers, not strings, for fixed sizes [OK]
Common Mistakes:
  • Using strings instead of numbers for width
  • Adding 'px' units which React Native does not use
  • Confusing flex with fixed width
3. What will be the height of the red box in this code?
import React from 'react';
import { View } from 'react-native';

export default function App() {
  return (
    
      
      
    
  );
}
medium
A. The red box will have height equal to flex 1.
B. The red box will fill all available space.
C. The red box will have height 0.
D. The red box will have height 100 pixels.

Solution

  1. Step 1: Analyze red box style

    The red box has height: 100, so it is fixed at 100 pixels tall.
  2. Step 2: Understand flex on sibling

    The blue box uses flex: 1 to fill remaining space, but red box height stays fixed.
  3. Final Answer:

    The red box will have height 100 pixels. -> Option D
  4. Quick Check:

    Fixed height overrides flex for that box [OK]
Hint: Fixed height overrides flex size [OK]
Common Mistakes:
  • Assuming flex affects fixed height components
  • Thinking red box fills all space
  • Confusing flex values with pixel sizes
4. Find the error in this React Native style code:
const styles = {
  container: {
    flex: '1',
    width: 200
  }
};
medium
A. flex should be a number, not a string.
B. width should be a string with 'px'.
C. flex cannot be used with width.
D. width must be less than 100.

Solution

  1. Step 1: Check flex value type

    In React Native, flex must be a number, not a string.
  2. Step 2: Validate width usage

    width: 200 is correct as a number; no 'px' needed.
  3. Final Answer:

    flex should be a number, not a string. -> Option A
  4. Quick Check:

    flex = number, not string [OK]
Hint: flex and width values must be numbers, not strings [OK]
Common Mistakes:
  • Using strings for flex or width
  • Adding 'px' units to width
  • Thinking flex and width cannot coexist
5. You want a container with two boxes: the first box fixed width 100 pixels, the second box fills the rest of the screen width. Which style setup achieves this?
hard
A. Parent: { flexDirection: 'column' }, Box1: { width: 100 }, Box2: { flex: 1 }
B. Parent: { flexDirection: 'row' }, Box1: { width: 100 }, Box2: { flex: 1 }
C. Parent: { flexDirection: 'row' }, Box1: { flex: 1 }, Box2: { width: 100 }
D. Parent: { flexDirection: 'column' }, Box1: { flex: 1 }, Box2: { width: 100 }

Solution

  1. Step 1: Set parent direction to row

    To place boxes side by side horizontally, use flexDirection: 'row'.
  2. Step 2: Assign fixed width and flexible width

    Box1 gets fixed width: 100, Box2 uses flex: 1 to fill remaining space.
  3. Final Answer:

    Parent: { flexDirection: 'row' }, Box1: { width: 100 }, Box2: { flex: 1 } -> Option B
  4. Quick Check:

    Row + fixed width + flex fill = Parent: { flexDirection: 'row' }, Box1: { width: 100 }, Box2: { flex: 1 } [OK]
Hint: Use row direction for horizontal layout with fixed + flex [OK]
Common Mistakes:
  • Using column direction for horizontal layout
  • Swapping flex and width on boxes
  • Forgetting flexDirection property
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard