0
0
React Nativemobile~20 mins

Width, height, and flex in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flex Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
How 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>
  );
}
AThe red view will be 100px wide, and the blue view will be 200px wide.
BBoth views will be 150px wide.
CBoth views will be 300px wide.
DThe red view will be 200px wide, and the blue view will be 100px wide.
Attempts:
2 left
💡 Hint
Flex values determine the ratio of space each child takes inside a flex container.
📝 Syntax
intermediate
1:30remaining
Which 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?
A{ height: 100, flexDirection: 'row' }
B{ height: 100, flex: 1 }
C{ width: 100, flex: 1 }
D{ height: 100, flexGrow: 0 }
Attempts:
2 left
💡 Hint
Flex controls how much space a component takes in the flex direction.
lifecycle
advanced
2:00remaining
What 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?
AThe width will be the minimum of 100 pixels and available space.
BThe width will expand to fill available space ignoring the 100 pixels.
CThe width will be 100 pixels, flex is ignored for width.
DThe width will be 100 pixels plus extra space from flex.
Attempts:
2 left
💡 Hint
Explicit width overrides flex sizing in React Native.
navigation
advanced
2:30remaining
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?
ALeft and right Views shrink, center View stays fixed.
BCenter View stays fixed size, left and right Views expand.
CAll three Views keep fixed widths regardless of screen size.
DCenter View expands or shrinks to fill space between left and right Views.
Attempts:
2 left
💡 Hint
Flex allows a View to take remaining space after fixed sizes.
🔧 Debug
expert
3:00remaining
Why 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>
  );
}
AThe child View height is 800 pixels, filling the parent.
BThe child View height is 0 because parent has fixed height but no flex.
CThe child View height is undefined and causes an error.
DThe child View height is less than 800 because flex needs parent flex to work.
Attempts:
2 left
💡 Hint
Flex:1 on a child fills the parent's defined height since every View is a flex container.