Discover how simple taps can become powerful actions with just a few lines of code!
Why Button and Pressable in React Native? - Purpose & Use Cases
Imagine you want to make a mobile app where users tap on things to do actions, like submitting a form or opening a menu.
You try to detect taps by watching every touch on the screen and guessing if it was on your text or image.
This manual way is slow and tricky. You have to write lots of code to check where the user touched, and it often misses taps or reacts late.
It's easy to make mistakes, and your app feels clunky and frustrating.
React Native's Button and Pressable components handle taps for you.
They know exactly when a user presses, holds, or releases, so you just say what should happen, and they do the hard work.
<View onTouchStart={handleTouch}>
<Text>Tap me</Text>
</View><Pressable onPress={handlePress}>
<Text>Tap me</Text>
</Pressable>You can create smooth, reliable buttons that respond instantly and look good on all devices without extra hassle.
Think of a shopping app where you tap a button to add items to your cart. Using Button or Pressable makes sure every tap counts and feels natural.
Manual tap detection is complicated and error-prone.
Button and Pressable simplify handling user taps.
They improve app responsiveness and user experience effortlessly.
Practice
Solution
Step 1: Understand the purpose of
ButtonButtonis designed for simple clickable buttons with default styles provided by React Native.Step 2: Compare with other components
Pressableis for custom touchable areas,Viewis a container, andTextInputis for text input, so they are not suitable for simple buttons.Final Answer:
Button-> Option AQuick Check:
Simple clickable button =Button[OK]
Button, custom ones use Pressable [OK]- Choosing Pressable for simple buttons
- Confusing View with clickable components
- Using TextInput for buttons
Pressable in a React Native file?Solution
Step 1: Recall React Native import syntax
React Native exports components likePressableas named exports, so we use curly braces in import.Step 2: Verify package name
The correct package isreact-native, notreact-native-pressable.Final Answer:
import { Pressable } from 'react-native'; -> Option AQuick Check:
Named import from 'react-native' = import { Pressable } from 'react-native'; [OK]
- Using default import instead of named import
- Importing from wrong package
- Omitting curly braces
import React from 'react';
import { Pressable, Text, Alert } from 'react-native';
export default function App() {
return (
Alert.alert('Pressed!')}>
Click me
);
}Solution
Step 1: Understand Pressable behavior
Pressablewraps theTextand triggersonPresscallback when pressed.Step 2: Check the onPress action
TheonPresscallsAlert.alert('Pressed!'), so an alert box will appear on press.Final Answer:
An alert with message 'Pressed!' appears when pressing the text. -> Option DQuick Check:
Pressable triggers alert on press = An alert with message 'Pressed!' appears when pressing the text. [OK]
- Thinking nothing happens on press
- Expecting automatic color change without style
- Confusing Button with Pressable behavior
Pressable:
import React from 'react';
import { Pressable, Text } from 'react-native';
export default function App() {
return (
console.log('Pressed')}>Click me
);
}Solution
Step 1: Check children of
In React Native, text must be wrapped inside aPressableTextcomponent; raw strings are not allowed as direct children.Step 2: Verify other parts
onPressis correctly defined, imports are correct, and returning a single component is valid.Final Answer:
Text must be wrapped in aTextcomponent insidePressable. -> Option BQuick Check:
Text inside Pressable needsTextwrapper [OK]
Text inside Pressable [OK]- Placing raw text directly inside Pressable
- Missing parentheses in onPress (not true here)
- Incorrect imports
Pressable to achieve this?Solution
Step 1: Understand dynamic styling with Pressable
Pressableaccepts a style function with apressedboolean to change styles when pressed.Step 2: Check each option
import { Pressable, Text } from 'react-native'; [{ backgroundColor: pressed ? 'gray' : 'white' }]}> Tap meuses the style function correctly to change background color on press.import { Button } from 'react-native'; ({ backgroundColor: pressed ? 'gray' : 'white' })} />incorrectly tries to styleButtonwith a function.import { Pressable, Text } from 'react-native'; alert('Tapped')}> Tap melacks style change.import { Pressable, Text } from 'react-native'; Tap mesets static gray background without press effect.Final Answer:
Pressable with style={({ pressed }) => [{ backgroundColor: pressed ? 'gray' : 'white' }]} -> Option CQuick Check:
Use style function with pressed for dynamic press styles [OK]
- Trying to style Button with style function
- Not wrapping text in Text component
- Using static styles without press state
Ready to prove it?
Pass the test and this pattern moves unknown
