TouchableOpacity is a component that makes an area respond to touches by reducing its opacity, giving a fade effect when pressed.
Click to reveal answer
beginner
How does TouchableHighlight visually respond to a press?
TouchableHighlight darkens or highlights its child view by applying a color overlay when pressed, making it look like it is highlighted.
Click to reveal answer
intermediate
When should you use TouchableOpacity instead of TouchableHighlight?
Use TouchableOpacity when you want a fade effect on press without changing the background color. It is good for buttons or icons that need subtle feedback.
Click to reveal answer
beginner
What prop controls the color shown when a TouchableHighlight is pressed?
The underlayColor prop sets the color overlay shown when the TouchableHighlight is pressed.
Click to reveal answer
beginner
Can TouchableOpacity and TouchableHighlight wrap any component?
Yes, both can wrap any React Native component to make it respond to touches with visual feedback.
Click to reveal answer
Which component fades the wrapped content when pressed?
ATouchableOpacity
BTouchableHighlight
CView
DScrollView
✗ Incorrect
TouchableOpacity reduces the opacity of its child components to create a fade effect on press.
What prop do you use to change the highlight color in TouchableHighlight?
AunderlayColor
BpressColor
CactiveColor
DhighlightColor
✗ Incorrect
The underlayColor prop sets the color shown when TouchableHighlight is pressed.
Which component is better for subtle press feedback without changing background color?
ATouchableHighlight
BTouchableOpacity
CButton
DTextInput
✗ Incorrect
TouchableOpacity provides a fade effect which is subtle and does not change background color.
Can TouchableHighlight wrap an Image component?
ANo
BOnly on iOS
COnly on Android
DYes
✗ Incorrect
TouchableHighlight can wrap any component, including Image, to provide touch feedback.
What happens if you press a TouchableHighlight without setting underlayColor?
AApp crashes
BNo visual feedback
CDefault highlight color is used
DOpacity changes
✗ Incorrect
TouchableHighlight uses a default underlay color if none is specified.
Explain the difference between TouchableOpacity and TouchableHighlight and when to use each.
Think about how the visual feedback looks when you press each component.
You got /4 concepts.
Describe how to customize the press feedback color in TouchableHighlight.
Look for a prop that controls the highlight color.
You got /3 concepts.
Practice
(1/5)
1. What is the main visual difference between TouchableOpacity and TouchableHighlight in React Native?
easy
A. Both components fade the button but with different speeds.
B. TouchableOpacity shows a colored highlight, TouchableHighlight fades the button.
C. TouchableOpacity fades the button, TouchableHighlight shows a colored highlight.
D. Both components show a colored highlight but with different colors.
Solution
Step 1: Understand TouchableOpacity behavior
TouchableOpacity reduces the opacity of the button to create a fading effect on tap.
Step 2: Understand TouchableHighlight behavior
TouchableHighlight adds a colored overlay highlight on tap instead of fading.
Final Answer:
TouchableOpacity fades the button, TouchableHighlight shows a colored highlight. -> Option C
Quick Check:
Opacity fade = TouchableOpacity, Highlight color = TouchableHighlight [OK]
Hint: Opacity fade means TouchableOpacity, colored highlight means TouchableHighlight [OK]
Common Mistakes:
Confusing which component fades vs highlights
Thinking both behave the same visually
Assuming TouchableHighlight fades opacity
2. Which of the following is the correct way to import TouchableOpacity in a React Native component?
easy
A. import { TouchableOpacity } from 'react-native';
B. import TouchableOpacity from 'react-native';
C. import TouchableOpacity from 'react-native-touchable';
D. import { TouchableOpacity } from 'react-native-touchable';
Solution
Step 1: Check React Native import syntax
React Native exports TouchableOpacity as a named export, so it must be imported with curly braces.
Step 2: Verify package source
TouchableOpacity is part of the core react-native package, not a separate package.
Final Answer:
import { TouchableOpacity } from 'react-native'; -> Option A
Quick Check:
Named import from 'react-native' = import { TouchableOpacity } from 'react-native'; [OK]
Hint: Use curly braces for named imports from 'react-native' [OK]
Common Mistakes:
Using default import without braces
Importing from wrong package name
Missing curly braces around TouchableOpacity
3. What will happen when the user taps the button in this code snippet?
import React from 'react';
import { TouchableHighlight, Text } from 'react-native';
export default function App() {
return (
alert('Pressed!')}>
Tap me
);
}
medium
A. The button fades out on tap and shows an alert.
B. The button shows a red highlight on tap and shows an alert.
C. Nothing happens on tap because underlayColor is invalid.
D. The button shows a yellow highlight on tap and shows an alert.
Solution
Step 1: Identify component behavior
TouchableHighlight uses the underlayColor prop to show a colored highlight when pressed.
Step 2: Check the underlayColor value and onPress
The underlayColor is set to 'yellow', so the highlight will be yellow. The onPress triggers an alert with 'Pressed!'.
Final Answer:
The button shows a yellow highlight on tap and shows an alert. -> Option D
Quick Check:
TouchableHighlight + underlayColor yellow + alert onPress = The button shows a yellow highlight on tap and shows an alert. [OK]
Hint: TouchableHighlight uses underlayColor for highlight color [OK]
Common Mistakes:
Confusing fade effect with TouchableOpacity
Assuming underlayColor changes text color
Ignoring the alert onPress effect
4. Identify the error in this React Native code using TouchableOpacity:
import React from 'react';
import { TouchableOpacity, Text } from 'react-native';
export default function App() {
return (
console.log('Pressed')}
Click me
);
}
medium
A. Missing closing bracket ">" on TouchableOpacity opening tag.
B. Incorrect import statement for TouchableOpacity.
C. The onPress function should return a value.
D. Text component cannot be a child of TouchableOpacity.
Solution
Step 1: Check JSX syntax for TouchableOpacity
The opening tag for TouchableOpacity is missing the closing ">" before the child Text component.
Step 2: Verify other parts
Imports are correct, onPress does not need to return a value, and Text can be a child of TouchableOpacity.
Final Answer:
Missing closing bracket ">" on TouchableOpacity opening tag. -> Option A
Quick Check:
JSX tags must close properly = Missing closing bracket ">" on TouchableOpacity opening tag. [OK]
Hint: Check JSX tags for missing > or /> [OK]
Common Mistakes:
Ignoring missing closing bracket errors
Thinking onPress must return a value
Believing Text cannot be child of TouchableOpacity
5. You want a button that shows a blue highlight when pressed and also reduces opacity slightly. Which approach correctly combines TouchableHighlight and TouchableOpacity to achieve this effect?
hard
A. Wrap TouchableHighlight inside TouchableOpacity with underlayColor='blue'.
B. Wrap TouchableOpacity inside TouchableHighlight with underlayColor='blue'.
C. Use only TouchableOpacity with underlayColor='blue' prop.
D. Use only TouchableHighlight with activeOpacity={0.5} prop.
Solution
Step 1: Understand each component's effect
TouchableHighlight provides the colored highlight via underlayColor. TouchableOpacity controls opacity fade via activeOpacity.
Step 2: Combine effects by nesting
Wrapping TouchableOpacity inside TouchableHighlight applies the highlight outside and opacity fade inside, combining both effects.
Step 3: Check other options
Wrap TouchableHighlight inside TouchableOpacity with underlayColor='blue'. reverses nesting, which breaks highlight effect. Options C and D misuse props not supported by those components.
Final Answer:
Wrap TouchableOpacity inside TouchableHighlight with underlayColor='blue'. -> Option B