Pressable and TextInput
You'll learn to make something tappable with Pressable, and to read what someone types with TextInput.
Watch & read
Start with the required ones. The others are there when you need them.
Explanation
You want a box where you type a new habit, like "Walk 20 minutes", and an "Add" button next to it. On a phone you need two things: a place to type, and something to tap.
Think of sending money in bKash. You type the amount into a box, and the screen shows each digit as you press it. Then you tap "Next", and only then does something happen. Typing and tapping are two separate jobs.
const [name, setName] = useState("");<TextInput value={name} onChangeText={setName} placeholder="New habit" /><Pressable onPress={() => console.log("Add", name)}><Text>Add</Text></Pressable>
TextInputis the box you type into.onChangeTextruns after every letter and gives you the whole text so far, as a plain string. Saving it in state withsetNamekeeps the box and your state the same.placeholderis the grey hint shown while the box is empty.Pressableis anything you can tap.onPressruns when the finger lifts. It has no look of its own, so you put aTextinside it and give it a style.styleon aPressablecan be a function:style={({ pressed }) => [styles.button, pressed && styles.down]}.pressedistruewhile the finger is down, so the button can change colour while you hold it.
On the web you wrote onChange and read e.target.value. onChangeText is simpler: it hands you the text
straight away.
People often write onPress={addHabit()} with brackets. That calls addHabit while the screen is
being drawn, not when you tap. Pass the function itself, onPress={addHabit}, or wrap it in an arrow,
onPress={() => addHabit(name)}.
When you read a form, find each TextInput and follow its onChangeText to a state variable. Then find the
Pressable and read its onPress: that is what happens on a tap.
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1export default function App() {2 const [name, setName] = useState("");3 return (4 <View style={{ padding: 24, gap: 8 }}>5 <TextInput value={name} onChangeText={setName} placeholder="Your name" />6 <Text>Hello, {name}</Text>7 </View>8 );9}10// You tap the box and type R, a, f, i. You don't press Enter.
1import { useState } from "react";2import { Pressable, StyleSheet, Text, TextInput, View } from "react-native";34export default function App() {5 const [name, setName] = useState("");6 const [habits, setHabits] = useState(["Drink water", "Read 10 pages"]);
Runs in the browser. Choose “My Device” in Snack to try it on your phone with Expo Go.
style={({ pressed }) => [styles.button, pressed && { opacity: 0.6 }]}.Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3What does
onChangeTextgive your function? - 2 / 3What is wrong with
<Pressable onPress={addHabit()}>? - 3 / 3You write
<Pressable onPress={save}>Save</Pressable>. What is the problem?
Explain it back
How confident are you with Pressable and TextInput?
Done
TextInput and a Pressable and say what happens when someone types and taps.