Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Core Components  ›  Lesson

TextInput

Text Input 12 minA controlled TextInput shows a value from state and reports every change as a string through onChangeText, with props to choose the keyboard and handle submission.

TextInput

Sprout's "Add plant" screen asks for a name and a watering interval in days. The user types, you store what they type, and a Save button uses it. On a phone that means choosing the right keyboard too. The component is TextInput.

The rule

Value in, onChangeText out, and pick the right keyboard.

A TextInput is usually controlled: its text lives in state, value shows it, and onChangeText updates it.

const [name, setName] = useState('');
const [days, setDays] = useState('');

<TextInput
  value={name}
  onChangeText={setName}
  placeholder="Plant name"
/>
<TextInput
  value={days}
  onChangeText={setDays}
  keyboardType="numeric"
  placeholder="Water every … days"
/>
  • onChangeText receives the new text as a plain string. There is no event.target.value to dig out as on the web.
  • keyboardType picks the on-screen keyboard: 'default', 'numeric', 'email-address', 'phone-pad' and more. A number field with the full keyboard annoys users.
  • secureTextEntry hides the characters, for passwords.
  • multiline allows several lines, for plant notes.
  • onSubmitEditing runs when the user presses the keyboard's return key, and returnKeyType="done" changes that key's label.
  • autoCapitalize and autoCorrect control the keyboard's helpers. Turn them off for email addresses.

Everything typed is a string, even with the numeric keyboard. Convert before you store it: Number(days).

maxLength caps how many characters can be typed, which suits a two-digit interval. To jump from the name field to the interval field, keep a ref to the second input and call its focus() method from the first input's onSubmitEditing.

A TextInput has no visible border on iOS by default, so give it a style with borderWidth and padding or it looks invisible.

The shortcut

For each field, answer three questions: where does its value live, what keyboard does it need, and what type do I need to store? Then write the props straight from the answers.

The watering-interval fieldStep 1 of 4
  1. Where does the value live?

    In state: days and setDays. So value={days} and onChangeText={setDays}.

Drills

Answer fast — trust the rule.

quick drill

What does onChangeText pass to its handler?

quick drill

Which prop shows a keyboard with only digits for the interval field?

quick drill

The user types 7 in the numeric field. What is stored in days?

quick drill

Which prop hides characters in a password field?

quick drill

Which prop runs save when the user presses the keyboard's return key?

Remember

  • Value in, onChangeText out, and pick the right keyboard.
  • Three questions: where the value lives, which keyboard, which type to store.
  • Trap: input is always a string, even from a numeric keyboard.

Practice — level up

0/1 solved

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

ExplainDesign an email field

Write the TextInput props you would use for an email field on a sign-up form, and explain each choice.