TouchableHighlight is a core React Native component used to make elements respond to touch. When pressed, it darkens or highlights its child view, giving visual feedback to the user.
onPressTouchableHighlight works by temporarily applying an underlay color to its child when the user presses it.
// Basic TouchableHighlight syntax
<TouchableHighlight onPress={handlePress} underlayColor="#DDDDDD">
<Text>Press Me</Text>
</TouchableHighlight>
// Simple button using TouchableHighlight
import React from "react";
import { View, Text, TouchableHighlight } from "react-native";
export default function App() {
const handlePress = () => {
console.log("Button pressed");
};
return (
<View>
<TouchableHighlight onPress={handlePress} underlayColor="#A5D8FF">
<Text>Tap Here</Text>
</TouchableHighlight>
</View>
);
}
When the user taps the text, the background briefly changes color and the message "Button pressed" appears in the console.
Use the controls below to simulate how underlayColor works in React Native. Press and hold the button to see the effect.
// How the simulation works conceptually
<TouchableHighlight
underlayColor="{selectedColor}"
onShowUnderlay={() => setBg(selectedColor)}
onHideUnderlay={() => setBg('white')}
onPress={() => log('Pressed')}
>
<View>Press Me</View>
</TouchableHighlight>
TouchableHighlightunderlayColor for good UXPressable for complex interactionsTouchableHighlight