We use controlled TextInput to keep track of what the user types. This helps us respond to input changes immediately.
TextInput with controlled state in React Native
Introduction
Syntax
React Native
const [text, setText] = useState(''); <TextInput value={text} onChangeText={setText} placeholder="Type here" />
The value prop sets the input's current text.
The onChangeText prop updates the state when the user types.
Examples
React Native
const [name, setName] = useState(''); <TextInput value={name} onChangeText={setName} placeholder="Enter your name" />
React Native
const [email, setEmail] = useState(''); <TextInput value={email} onChangeText={setEmail} keyboardType="email-address" placeholder="Enter your email" />
Sample App
This app shows a text input box. Whatever you type appears below in real time. The input is controlled by React state.
React Native
import React, { useState } from 'react'; import { View, TextInput, Text, StyleSheet } from 'react-native'; export default function App() { const [text, setText] = useState(''); return ( <View style={styles.container}> <TextInput style={styles.input} value={text} onChangeText={setText} placeholder="Type something" accessibilityLabel="Input field" /> <Text style={styles.output}>You typed: {text}</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 20, backgroundColor: '#fff' }, input: { height: 40, borderColor: '#888', borderWidth: 1, paddingHorizontal: 10, marginBottom: 20, borderRadius: 5 }, output: { fontSize: 16 } });
Important Notes
Always set value and onChangeText together to keep input controlled.
Controlled inputs help keep your app data in sync with what the user sees.
Use accessibilityLabel for better screen reader support.
Summary
Controlled TextInput means React state holds the input text.
Use value and onChangeText props to control input.
This lets you respond instantly to user typing and keep UI updated.
Practice
1. What does it mean when a
TextInput in React Native is "controlled"?easy
Solution
Step 1: Understand controlled input concept
A controlledTextInputmeans React state holds the current text value.Step 2: Identify how React manages input
React usesvalueprop to set input text andonChangeTextto update state on typing.Final Answer:
The input value is managed by React state usingvalueandonChangeText. -> Option AQuick Check:
Controlled input = React state control [OK]
Hint: Controlled means React state controls input text [OK]
Common Mistakes:
- Thinking controlled means input is read-only
- Confusing controlled with auto-saving
- Assuming native keyboard controls the value
2. Which of the following is the correct way to create a controlled
TextInput in React Native?easy
Solution
Step 1: Identify correct props for controlled input
Thevalueprop sets the input text, andonChangeTextupdates state on typing.Step 2: Check each option's syntax
<TextInput value={text} onChangeText={setText} />usesvalueandonChangeTextcorrectly. Others misuseonChangeordefaultValue.Final Answer:
<TextInput value={text} onChangeText={setText} /> -> Option BQuick Check:
Use value + onChangeText for controlled input [OK]
Hint: Use value and onChangeText props for controlled TextInput [OK]
Common Mistakes:
- Using onChange instead of onChangeText
- Using defaultValue for controlled input
- Passing wrong handler to onChangeText
3. What will be displayed inside the
TextInput after typing "Hi" in this code?
const [text, setText] = React.useState('');
<TextInput value={text} onChangeText={setText} />medium
Solution
Step 1: Understand state updates on typing
Typing "Hi" triggersonChangeTextwhich callssetTextto update state to "Hi".Step 2: Value prop reflects state
Thevalueprop usestextstate, so input shows "Hi" as typed.Final Answer:
The input will show "Hi" as typed. -> Option CQuick Check:
State updates input value = typed text [OK]
Hint: Value prop shows current state text instantly [OK]
Common Mistakes:
- Thinking input won't update without defaultValue
- Confusing onChangeText with onChange
- Assuming input clears after typing
4. Identify the error in this controlled
TextInput code:
const [text, setText] = React.useState('');
<TextInput value={text} onChange={setText} />medium
Solution
Step 1: Check event handler prop
TextInputusesonChangeTextfor text changes, notonChange.Step 2: Effect of wrong handler
UsingonChangemeanssetTextnever runs, so state stays empty and input doesn't update.Final Answer:
UsingonChangeinstead ofonChangeTextcauses no updates. -> Option AQuick Check:
Use onChangeText, not onChange, for TextInput [OK]
Hint: Use onChangeText, not onChange, for TextInput updates [OK]
Common Mistakes:
- Confusing onChange with onChangeText
- Thinking defaultValue is required for controlled input
- Removing value prop breaks control
5. You want to create a controlled
TextInput that only accepts uppercase letters. Which code snippet correctly implements this behavior?hard
Solution
Step 1: Understand uppercase enforcement
To force uppercase, transform input text to uppercase before saving state.Step 2: Check each option's logic
const [text, setText] = React.useState(''); <TextInput value={text} onChangeText={t => setText(t.toUpperCase())} />converts input to uppercase inonChangeTextbefore callingsetText.const [text, setText] = React.useState(''); <TextInput value={text.toUpperCase()} onChangeText={setText} />only uppercases value but stores raw input, so user can type lowercase.const [text, setText] = React.useState(''); <TextInput defaultValue={text.toUpperCase()} onChangeText={setText} />usesdefaultValuewhich is uncontrolled.const [text, setText] = React.useState(''); <TextInput value={text} onChangeText={t => setText(t.toLowerCase())} />converts to lowercase, opposite of requirement.Final Answer:
<TextInput value={text} onChangeText={t => setText(t.toUpperCase())} /> -> Option DQuick Check:
Transform input in onChangeText for controlled uppercase [OK]
Hint: Transform text in onChangeText before setting state [OK]
Common Mistakes:
- Trying to uppercase value prop only
- Using defaultValue instead of value
- Transforming text to lowercase by mistake
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
