Discover how to make your app's buttons feel alive with just a simple wrapper!
Why TouchableOpacity and TouchableHighlight in React Native? - Purpose & Use Cases
Imagine you want to make a button in your app that changes color or looks different when you press it, just like a real button does in everyday life.
You try to do this by manually changing styles every time the user touches the button.
Doing this by hand means writing lots of code to track when the button is pressed and released.
This can get messy, slow, and easy to make mistakes.
You might forget to reset the style or make the button flicker in a weird way.
TouchableOpacity and TouchableHighlight are ready-made tools that handle all the touch feedback for you.
They automatically change the button's look when pressed, making your app feel smooth and natural without extra work.
const [pressed, setPressed] = useState(false);
<View
onTouchStart={() => setPressed(true)}
onTouchEnd={() => setPressed(false)}
style={{backgroundColor: pressed ? 'gray' : 'blue'}}>
<Text>Press me</Text>
</View><TouchableOpacity onPress={() => Alert.alert('Pressed!')}>
<Text>Press me</Text>
</TouchableOpacity>It lets you create buttons and touchable elements that feel responsive and polished with almost no extra code.
Think about a shopping app where you tap "Add to Cart" and the button dims slightly to show your tap was registered instantly.
Manually handling touch feedback is complicated and error-prone.
TouchableOpacity and TouchableHighlight simplify this by managing touch states automatically.
Using them makes your app feel smooth and professional with less code.
Practice
TouchableOpacity and TouchableHighlight in React Native?Solution
Step 1: Understand
TouchableOpacitybehaviorTouchableOpacityreduces the opacity of the button to create a fading effect on tap.Step 2: Understand
TouchableHighlightbehaviorTouchableHighlightadds a colored overlay highlight on tap instead of fading.Final Answer:
TouchableOpacityfades the button,TouchableHighlightshows a colored highlight. -> Option CQuick Check:
Opacity fade = TouchableOpacity, Highlight color = TouchableHighlight [OK]
- Confusing which component fades vs highlights
- Thinking both behave the same visually
- Assuming TouchableHighlight fades opacity
TouchableOpacity in a React Native component?Solution
Step 1: Check React Native import syntax
React Native exportsTouchableOpacityas a named export, so it must be imported with curly braces.Step 2: Verify package source
TouchableOpacityis part of the corereact-nativepackage, not a separate package.Final Answer:
import { TouchableOpacity } from 'react-native'; -> Option AQuick Check:
Named import from 'react-native' = import { TouchableOpacity } from 'react-native'; [OK]
- Using default import without braces
- Importing from wrong package name
- Missing curly braces around TouchableOpacity
import React from 'react';
import { TouchableHighlight, Text } from 'react-native';
export default function App() {
return (
alert('Pressed!')}>
Tap me
);
}Solution
Step 1: Identify component behavior
TouchableHighlightuses theunderlayColorprop 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 DQuick Check:
TouchableHighlight + underlayColor yellow + alert onPress = The button shows a yellow highlight on tap and shows an alert. [OK]
- Confusing fade effect with TouchableOpacity
- Assuming underlayColor changes text color
- Ignoring the alert onPress effect
TouchableOpacity:
import React from 'react';
import { TouchableOpacity, Text } from 'react-native';
export default function App() {
return (
console.log('Pressed')}
Click me
);
}Solution
Step 1: Check JSX syntax for
The opening tag forTouchableOpacityTouchableOpacityis missing the closing ">" before the childTextcomponent.Step 2: Verify other parts
Imports are correct,onPressdoes not need to return a value, andTextcan be a child ofTouchableOpacity.Final Answer:
Missing closing bracket ">" onTouchableOpacityopening tag. -> Option AQuick Check:
JSX tags must close properly = Missing closing bracket ">" onTouchableOpacityopening tag. [OK]
- Ignoring missing closing bracket errors
- Thinking onPress must return a value
- Believing Text cannot be child of TouchableOpacity
TouchableHighlight and TouchableOpacity to achieve this effect?Solution
Step 1: Understand each component's effect
TouchableHighlightprovides the colored highlight viaunderlayColor.TouchableOpacitycontrols opacity fade viaactiveOpacity.Step 2: Combine effects by nesting
WrappingTouchableOpacityinsideTouchableHighlightapplies the highlight outside and opacity fade inside, combining both effects.Step 3: Check other options
WrapTouchableHighlightinsideTouchableOpacitywithunderlayColor='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 BQuick Check:
Nesting TouchableOpacity inside TouchableHighlight = WrapTouchableOpacityinsideTouchableHighlightwithunderlayColor='blue'. [OK]
- Reversing component nesting order
- Using unsupported props on components
- Expecting one component to do both effects alone
Ready to prove it?
Pass the test and this pattern moves unknown
