What is the purpose of the View component in React Native?
The View component is a container that supports layout with Flexbox, style, and touch handling. It is like a div in web development and is used to group other components.
Click to reveal answer
beginner
How do you apply styles to a View component?
You apply styles by passing a style prop with an object or array of objects defining layout, colors, size, and more. For example: <View style={{flex: 1, backgroundColor: 'blue'}} />.
Click to reveal answer
beginner
Which layout system does the View component use by default?
The View component uses Flexbox layout by default, which helps arrange child components in rows or columns with flexible sizing.
Click to reveal answer
intermediate
Can a View component handle user touches?
Yes, View can handle touches by using props like onStartShouldSetResponder and onResponderRelease. However, for simpler touch handling, components like TouchableOpacity are often used.
Click to reveal answer
intermediate
What is the difference between View and ScrollView?
View is a basic container for layout and styling, while ScrollView is a special container that allows scrolling when content is larger than the screen.
Click to reveal answer
What is the default layout system used by the View component?
AAbsolute positioning
BGrid
CFlexbox
DTable layout
✗ Incorrect
The View component uses Flexbox layout by default to arrange child components.
Which prop is used to style a View component?
AclassName
Blayout
Ccss
Dstyle
✗ Incorrect
The style prop is used to apply styles to a View component.
Which component should you use if you want a container that scrolls when content is too big?
AScrollView
BView
CText
DImage
✗ Incorrect
ScrollView allows scrolling when content overflows the screen.
Can a View component handle touch events?
AYes, with responder props
BNo, never
COnly with <code>TouchableOpacity</code>
DOnly on Android
✗ Incorrect
View can handle touch events using responder props like onStartShouldSetResponder.
Which of these is NOT a typical use of View?
AGrouping components
BDisplaying text content
CApplying layout and styles
DHandling touch gestures
✗ Incorrect
Text component is used for displaying text, not View.
Explain what the View component is and why it is important in React Native.
Think about how you organize things in a box or frame.
You got /4 concepts.
Describe how you would style a View to have a blue background and take all available space.
Remember the style prop takes an object with CSS-like properties.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the View component in React Native?
easy
A. To act as a container for layout and styling
B. To display text content
C. To handle user input events
D. To fetch data from the internet
Solution
Step 1: Understand the role of View
The View component is designed to hold other components and arrange them visually.
Step 2: Differentiate from other components
Text displays text, input handles user input, and data fetching is done elsewhere, not by View.
Final Answer:
To act as a container for layout and styling -> Option A
Quick Check:
View = container for layout [OK]
Hint: View is like a box to hold and arrange items [OK]
Common Mistakes:
Confusing View with Text component
Thinking View handles user input directly
Assuming View fetches data
2. Which of the following is the correct way to create a View with a red background in React Native?
easy
A.
B.
C.
D.
Solution
Step 1: Check React Native style syntax
React Native uses a style prop with an object, e.g., style={{backgroundColor: 'red'}}.
Step 2: Identify incorrect syntax
Props like bgColor or background are not valid; style as string is invalid in React Native.
Final Answer:
<View style={{backgroundColor: 'red'}}></View> -> Option C
Quick Check:
Use style={{backgroundColor: 'color'}} [OK]
Hint: Use style={{backgroundColor: 'color'}} for colors [OK]
Common Mistakes:
Using HTML style string instead of object
Using invalid props like bgColor
Forgetting double curly braces for style
3. What will be the visible result of this code?
import React from 'react';
import { View, Text } from 'react-native';
export default function App() {
return (
One
Two
Three
);
}
medium
A. The texts appear stacked vertically
B. The texts 'One', 'Two', 'Three' appear side by side horizontally
C. Only 'One' is visible
D. The app crashes due to syntax error
Solution
Step 1: Understand flexDirection in View
Setting flexDirection to 'row' arranges children horizontally in a row.
Step 2: Check children components
Three Text components will appear side by side: 'One', 'Two', 'Three'.
Final Answer:
The texts 'One', 'Two', 'Three' appear side by side horizontally -> Option B
Quick Check:
flexDirection: 'row' = horizontal layout [OK]
Hint: flexDirection: 'row' arranges children horizontally [OK]
Common Mistakes:
Assuming default vertical stacking
Confusing flexDirection values
Thinking only one child shows
4. Identify the error in this React Native code snippet:
return (
First
Second
Third
Outside
);
medium
A. Multiple root elements without a wrapper
B. Invalid style property 'flexDirection'
C. Text components cannot be children of View
D. Missing import for Text component
Solution
Step 1: Check JSX root elements
JSX must return a single root element; here, View and Text are siblings without a wrapper.
Step 2: Validate style and imports
flexDirection: 'column' is valid; Text inside View is allowed; assume imports are correct.
Final Answer:
Multiple root elements without a wrapper -> Option A
Quick Check:
JSX needs one root element [OK]
Hint: Wrap siblings in one parent View or Fragment [OK]
Common Mistakes:
Trying to return multiple root elements
Misunderstanding flexDirection usage
Assuming Text can't be inside View
5. You want to create a View that fills the entire screen and centers its child Text both vertically and horizontally. Which style object achieves this?
hard
A. {flexGrow: 0, justifyContent: 'flex-start', alignItems: 'flex-start'}
B. {width: '100%', height: '100%', textAlign: 'center'}
C. {position: 'absolute', top: 0, left: 0}
D. {flex: 1, justifyContent: 'center', alignItems: 'center'}
Solution
Step 1: Make View fill screen
Using flex: 1 makes the View expand to fill available space.
Step 2: Center children vertically and horizontally