Buttons and Controls

Loading concept...

๐ŸŽฎ 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 button
  • onPress = What happens when you tap
  • color = 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 tap
  • activeOpacity = How faded it gets (0 to 1)
    • 0.6 = fades to 60% visible
    • 1 = 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 = true when finger is on button
  • pressed = false when 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

  1. Always give feedback - User should FEEL the tap
  2. 44px minimum - Make touch targets finger-friendly
  3. Pressable is your best friend - Most flexible option
  4. 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! ๐ŸŽ‰

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.