๐ฎ Buttons and Controls in React Native
The Remote Control of Your App
Imagine you have a TV remote. Every button does something special - one changes channels, one adjusts volume, one turns the TV on or off.
Your React Native app is just like that TV. And buttons are the remote control!
Without buttons, your app would just be a pretty picture. Users couldnโt do anything. Buttons bring your app to life!
๐ต The Button Component
Your First Friend - Simple and Ready
Think of the Button component like the big red โPLAYโ button on a toy. You press it, something happens. No fancy stuff - just works!
<Button
title="Press Me!"
onPress={() => console.log('Hello!')}
color="#841584"
/>
Whatโs happening here?
title= The words on the buttononPress= What happens when you tapcolor= The buttonโs color
โ ๏ธ The Catch
Button is super easy, BUT it looks different on iPhone vs Android. You canโt change its shape or add icons. Itโs like a pre-made sandwich - you canโt swap ingredients.
Use Button when: You need something quick and simple.
๐ TouchableOpacity - The Fading Touch
Everything Can Be a Button!
What if you want a picture to be tappable? Or text? Or anything?
TouchableOpacity is magic! Wrap anything inside it, and poof - itโs touchable!
When you press it, it fades a little (becomes see-through). This tells the user โYes! I felt that tap!โ
<TouchableOpacity
onPress={() => alert('You tapped!')}
activeOpacity={0.6}
>
<Text>Tap this text!</Text>
<Image source={myPic} />
</TouchableOpacity>
Key Parts:
onPress= What happens on tapactiveOpacity= How faded it gets (0 to 1)0.6= fades to 60% visible1= no fade at all
Why Use This?
You get full control over how your button looks. Make it round, square, colorful - anything!
โก Pressable - The Swiss Army Knife
The Newest and Most Powerful
Pressable is like having superpowers! It can detect:
- When your finger starts touching
- When you let go
- When you press for a long time
- When your finger moves away
<Pressable
onPress={() => console.log('Quick tap!')}
onLongPress={() => console.log('Held it down!')}
onPressIn={() => console.log('Finger down!')}
onPressOut={() => console.log('Finger lifted!')}
>
<Text>Try different touches!</Text>
</Pressable>
The Flow of a Press
graph TD A[Finger Touches Screen] --> B[onPressIn fires] B --> C{How long?} C -->|Quick tap| D[onPress fires] C -->|Held 500ms+| E[onLongPress fires] D --> F[onPressOut fires] E --> F F --> G[Finger Leaves Screen]
๐จ Pressable States - Changing How It Looks
Make It React to Touch!
When someone presses your button, it should show itโs being pressed. Pressable gives you a special power called style function.
<Pressable
style={({ pressed }) => [
{
backgroundColor: pressed
? '#ddd'
: '#fff'
}
]}
>
<Text>Press to see me change!</Text>
</Pressable>
Whatโs pressed?
pressed = truewhen finger is on buttonpressed = falsewhen not touching
Visual Feedback Recipe
| State | What to Change |
|---|---|
| Pressed | Darker background |
| Pressed | Slightly smaller |
| Pressed | Add shadow |
| Pressed | Change text color |
style={({ pressed }) => ({
backgroundColor: pressed ? '#e0e0e0' : '#fff',
transform: [{ scale: pressed ? 0.98 : 1 }],
opacity: pressed ? 0.8 : 1,
})}
๐ Touch Target Sizing - Make It Easy to Tap!
The 44px Rule
Imagine trying to press a tiny ant with your finger. Hard, right?
Apple says: Every tappable thing should be at least 44x44 pixels.
Thatโs about the size of your fingertip!
The Problem
Sometimes your button looks small, but you still want it easy to tap.
The Solution: hitSlop
hitSlop makes the invisible tappable area BIGGER than what you see!
<Pressable
hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
onPress={() => console.log('Easy to tap!')}
>
<Text style={{ fontSize: 12 }}>Small text</Text>
</Pressable>
graph TD A[Visible Button: 30x30px] --> B[hitSlop adds 10px each side] B --> C[Actual Touch Area: 50x50px] C --> D[Easy for fingers!]
Pro Tip
Even if your icon is 24x24, make the touch area 44x44 or bigger!
๐ Switch Component - On/Off Toggle
Like a Light Switch!
A Switch is for yes/no choices. Dark mode? On or off. Notifications? On or off.
const [isOn, setIsOn] = useState(false);
<Switch
value={isOn}
onValueChange={(newValue) => setIsOn(newValue)}
trackColor={{ false: '#767577', true: '#81b0ff' }}
thumbColor={isOn ? '#f5dd4b' : '#f4f3f4'}
/>
The Parts
| Part | What It Is |
|---|---|
value |
Is it on or off? (true/false) |
onValueChange |
Function when it changes |
trackColor |
The background bar color |
thumbColor |
The circle you slide |
Visual Breakdown
OFF State: ON State:
โโโโโโโโโโโโ โโโโโโโโโโโโ
โ gray โ โ blue โ
โโโโโโโโโโโโ โโโโโโโโโโโโ
false true
๐ RefreshControl - Pull to Refresh
Like Magic at Your Fingertip!
You know when you pull down on Instagram and it loads new posts? Thatโs RefreshControl!
It only works inside scrollable things like ScrollView or FlatList.
const [refreshing, setRefreshing] = useState(false);
const onRefresh = async () => {
setRefreshing(true);
await fetchNewData();
setRefreshing(false);
};
<ScrollView
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={onRefresh}
colors={['#ff0000', '#00ff00']}
tintColor="#0000ff"
/>
}
>
{/* Your content */}
</ScrollView>
How It Works
graph TD A[User Pulls Down] --> B[onRefresh Called] B --> C[refreshing = true] C --> D[Spinner Shows] D --> E[Load New Data] E --> F[refreshing = false] F --> G[Spinner Hides]
Key Props
| Prop | What It Does |
|---|---|
refreshing |
Show spinner? (true/false) |
onRefresh |
Function to run |
colors |
Android spinner colors |
tintColor |
iOS spinner color |
๐ฏ Quick Comparison: Which One to Use?
| Need | Use This |
|---|---|
| Quick, simple button | Button |
| Custom look with fade | TouchableOpacity |
| Full control, many events | Pressable |
| On/Off toggle | Switch |
| Pull-down refresh | RefreshControl |
๐ Remember These Golden Rules
- Always give feedback - User should FEEL the tap
- 44px minimum - Make touch targets finger-friendly
- Pressable is your best friend - Most flexible option
- Test on real devices - Fingers are different from mouse!
๐ You Did It!
You now know ALL the ways to make your React Native app interactive!
- Button = Quick and easy
- TouchableOpacity = Custom looks with fade
- Pressable = Super powers with states
- Switch = On/Off toggles
- RefreshControl = Pull to update
Go build something amazing! Every great app is just buttons waiting to be pressed! ๐