← Back to Chapters

TouchableOpacity

? TouchableOpacity

? Quick Overview

TouchableOpacity is a React Native component used to make any view respond to touch events by reducing its opacity when pressed.

? Key Concepts

  • Provides visual feedback by fading opacity
  • Wraps text, views, or icons
  • Supports press events like onPress
  • Lightweight and commonly used

? Syntax / Theory

The component wraps child elements and triggers an action when pressed.

? View Code Example
// Basic TouchableOpacity structure
<TouchableOpacity onPress={handlePress}>
<Text>Click Me</Text>
</TouchableOpacity>

? Code Example(s)

? View Code Example
// Simple button using TouchableOpacity
import React from "react";
import { View, Text, TouchableOpacity } from "react-native";

export default function App() {
return (
<View>
<TouchableOpacity onPress={() => alert("Button Pressed")}>
<Text>Press Here</Text>
</TouchableOpacity>
</View>
);
}

? Live Output / Explanation

What happens?

When the user taps the text, the opacity reduces briefly and an alert message appears.

? Interactive Example

Imagine a mobile button that fades when touched — this feedback improves user experience.

Click or Tap the items below to see the opacity change in action:

My Mobile App
Submit Action
?
View User Profile
⚙️
Settings
> Console Ready...

? Use Cases

  • Custom buttons
  • Clickable cards
  • List items
  • Icons with actions

? Tips & Best Practices

  • Use activeOpacity to control fade intensity
  • Avoid nesting too many touchables
  • Use for simple interactions

? Try It Yourself

  • Create a colored button using TouchableOpacity
  • Change opacity using activeOpacity
  • Trigger navigation on press