Challenge - 5 Problems
Flex Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediateHow does flex affect component size?
Consider a parent View with two child Views. The first child has style { flex: 1 }, the second child has style { flex: 2 }. How will their widths compare if the parent is a row container?
React Native
import React from 'react'; import { View } from 'react-native'; export default function App() { return ( <View style={{ flexDirection: 'row', width: 300, height: 100 }}> <View style={{ flex: 1, backgroundColor: 'red' }} /> <View style={{ flex: 2, backgroundColor: 'blue' }} /> </View> ); }
Attempts:
2 left
💡 Hint
Flex values determine the ratio of space each child takes inside a flex container.
✗ Incorrect
The parent has width 300. The first child with flex 1 takes 1 part, the second child with flex 2 takes 2 parts. Total parts = 3. So widths are 300 * 1/3 = 100 and 300 * 2/3 = 200.
📝 Syntax
intermediateWhich style correctly sets fixed height and flexible width?
You want a View that is always 100 pixels tall but stretches horizontally to fill available space. Which style achieves this?
Attempts:
2 left
💡 Hint
Flex controls how much space a component takes in the flex direction.
✗ Incorrect
Setting height fixes vertical size. Setting flex:1 makes the component expand to fill available space horizontally in a flex container.
❓ lifecycle
advancedWhat happens if you set both width and flex on a View?
If a View has style { width: 100, flex: 1 } inside a flex container, what will be its width?
Attempts:
2 left
💡 Hint
Explicit width overrides flex sizing in React Native.
✗ Incorrect
When width is set explicitly, it takes priority over flex for that dimension. Flex only affects size if width or height is not fixed.
advanced
How does flex affect nested Views in navigation header?
In a navigation header with flexDirection 'row', you have three Views: left, center, right. Center has flex: 1, left and right have fixed widths. What happens to the center View when screen width changes?
Attempts:
2 left
💡 Hint
Flex allows a View to take remaining space after fixed sizes.
✗ Incorrect
The center View with flex:1 fills the space left by fixed width left and right Views, adjusting as screen width changes.
🔧 Debug
expertWhy does this View not fill the screen height?
You have a View with style { flex: 1 } inside a parent View without flex. The screen height is 800 pixels. What is the height of the child View?
React Native
import React from 'react'; import { View } from 'react-native'; export default function App() { return ( <View style={{ height: 800 }}> <View style={{ flex: 1, backgroundColor: 'green' }} /> </View> ); }
Attempts:
2 left
💡 Hint
Flex:1 on a child fills the parent's defined height since every View is a flex container.
✗ Incorrect
The parent View with height: 800 is a flex container (default flexDirection: 'column'). The child with flex: 1 grows to fill the entire available space of 800px.
