Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Core Components  ›  Lesson

Pressable, Button and Switch

Button 12 minButton is a quick platform-styled action, Pressable makes any content touchable with full control over its look and press feedback, and Switch is a controlled on/off toggle.

Pressable, Button and Switch

Sprout needs three kinds of touch: a quick "Watered" button, a whole plant card you can tap to open, and an on/off setting for reminders. React Native has a component for each, and choosing the right one decides how much control you get over the look.

The rule

Button for quick, Pressable for custom, Switch for on and off.

Button is the simplest. It draws the platform's standard button and accepts very little styling:

<Button title="Watered" onPress={markWatered} color="#2e7d32" />

It takes a title string, an onPress function, a color and a disabled flag. You cannot give it a custom shape or padding, so real apps soon outgrow it.

Pressable wraps anything and makes it respond to touch. Its style can be a function that receives { pressed }, so you can show feedback while the finger is down:

<Pressable
  onPress={() => openPlant(plant.id)}
  onLongPress={() => askToDelete(plant.id)}
  hitSlop={8}
  style={({ pressed }) => [styles.card, pressed && { opacity: 0.6 }]}
>
  <Text>{plant.name}</Text>
</Pressable>
  • onPress fires on a normal tap; onLongPress fires when held (about half a second by default).
  • hitSlop enlarges the touchable area beyond the visible box, which helps small icons.
  • Older code uses TouchableOpacity and similar Touchables. They still work, but Pressable is the more flexible, recommended choice for new code.

Switch is a toggle. Like a controlled input, it shows a value and reports changes:

const [remind, setRemind] = useState(true);

<Switch value={remind} onValueChange={setRemind} />

onValueChange receives the new boolean. If you forget to update state, the switch snaps back, because value still says the old thing.

The shortcut

Ask "what is the touch for?" A plain action with a label: Button. Anything that needs its own look, or a whole area being tappable: Pressable. A setting that is either on or off: Switch.

Choosing touch components for SproutStep 1 of 4
  1. "Watered" on a settings test screen

    Plain action, default look is fine. Button.

Drills

Answer fast — trust the rule.

quick drill

You need a round, green "Watered" button with an icon. Which component fits?

quick drill

What does onValueChange on a Switch receive?

quick drill

A Switch snaps back every time you flip it. What is the likely bug?

quick drill

How can a Pressable fade while it is being pressed?

quick drill

Which prop makes a small icon easier to tap without making it look bigger?

Remember

  • Button for quick, Pressable for custom, Switch for on and off.
  • Ask "what is the touch for?"
  • Trap: a controlled Switch whose state never updates snaps back.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPick the touch component

For a settings screen, choose a component for (a) a 'Dark mode' toggle, (b) a custom-styled 'Save' button with an icon, and (c) a plain 'Reset' action on a debug screen. Explain each choice.