Bird
Raised Fist0
React Nativemobile~15 mins

TextInput with controlled state in React Native - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Controlled TextInput Screen
A screen with a text input field that shows the typed text below it in real time.
Target UI
-------------------------
| Controlled TextInput  |
|-----------------------|
| Enter text:           |
| [___________________] |
|                       |
| You typed:            |
|                       |
|                       |
-------------------------
Add a TextInput component with a placeholder 'Enter text:'
Use state to control the TextInput value
Display the typed text below the TextInput in a Text component
Update the displayed text as the user types
Starter Code
React Native
import React, { useState } from 'react';
import { View, TextInput, Text, StyleSheet } from 'react-native';

export default function ControlledTextInput() {
  // TODO: Add state for text input

  return (
    <View style={styles.container}>
      {/* TODO: Add TextInput with controlled value and onChangeText */}
      {/* TODO: Display typed text below */}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    justifyContent: 'center',
  },
  input: {
    borderWidth: 1,
    borderColor: '#888',
    padding: 10,
    fontSize: 18,
    borderRadius: 5,
  },
  typedText: {
    marginTop: 20,
    fontSize: 18,
  },
});
Task 1
Task 2
Task 3
Solution
React Native
import React, { useState } from 'react';
import { View, TextInput, Text, StyleSheet } from 'react-native';

export default function ControlledTextInput() {
  const [text, setText] = useState('');

  return (
    <View style={styles.container}>
      <TextInput
        style={styles.input}
        placeholder="Enter text:"
        value={text}
        onChangeText={setText}
        accessibilityLabel="Text input field"
      />
      <Text style={styles.typedText} accessibilityLiveRegion="polite">
        You typed: {text}
      </Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    justifyContent: 'center',
  },
  input: {
    borderWidth: 1,
    borderColor: '#888',
    padding: 10,
    fontSize: 18,
    borderRadius: 5,
  },
  typedText: {
    marginTop: 20,
    fontSize: 18,
  },
});

We use React's useState hook to keep track of the text input's value. The TextInput component's value prop is set to this state, making it a controlled input. The onChangeText prop updates the state whenever the user types. Below the input, a Text component shows the current typed text, updating live as the user types. Accessibility labels and live region are added for screen reader support.

Final Result
Completed Screen
-------------------------
| Controlled TextInput  |
|-----------------------|
| Enter text:           |
| [Hello World______]   |
|                       |
| You typed:            |
| Hello World           |
|                       |
-------------------------
User taps the TextInput and types text
Typed text appears live below the input
Screen reader announces the input label and updates the typed text
Stretch Goal
Add a clear button inside the TextInput to erase all text
💡 Hint
Use a TouchableOpacity with an 'X' icon inside the TextInput container and clear the state on press

Practice

(1/5)
1. What does it mean when a TextInput in React Native is "controlled"?
easy
A. The input value is managed by React state using value and onChangeText.
B. The input automatically saves data to a database.
C. The input cannot be edited by the user.
D. The input uses default native keyboard settings only.

Solution

  1. Step 1: Understand controlled input concept

    A controlled TextInput means React state holds the current text value.
  2. Step 2: Identify how React manages input

    React uses value prop to set input text and onChangeText to update state on typing.
  3. Final Answer:

    The input value is managed by React state using value and onChangeText. -> Option A
  4. Quick 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
A. <TextInput value={text} onChange={setText} />
B. <TextInput value={text} onChangeText={setText} />
C. <TextInput defaultValue={text} onChange={setText} />
D. <TextInput defaultValue={text} onChangeText={text} />

Solution

  1. Step 1: Identify correct props for controlled input

    The value prop sets the input text, and onChangeText updates state on typing.
  2. Step 2: Check each option's syntax

    <TextInput value={text} onChangeText={setText} /> uses value and onChangeText correctly. Others misuse onChange or defaultValue.
  3. Final Answer:

    <TextInput value={text} onChangeText={setText} /> -> Option B
  4. Quick 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
A. The input will show "undefined".
B. The input will remain empty.
C. The input will show "Hi" as typed.
D. The input will show "" (empty string) always.

Solution

  1. Step 1: Understand state updates on typing

    Typing "Hi" triggers onChangeText which calls setText to update state to "Hi".
  2. Step 2: Value prop reflects state

    The value prop uses text state, so input shows "Hi" as typed.
  3. Final Answer:

    The input will show "Hi" as typed. -> Option C
  4. Quick 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
A. Using onChange instead of onChangeText causes no updates.
B. State should be initialized with null, not empty string.
C. Missing defaultValue prop causes error.
D. The value prop should be omitted for controlled input.

Solution

  1. Step 1: Check event handler prop

    TextInput uses onChangeText for text changes, not onChange.
  2. Step 2: Effect of wrong handler

    Using onChange means setText never runs, so state stays empty and input doesn't update.
  3. Final Answer:

    Using onChange instead of onChangeText causes no updates. -> Option A
  4. Quick 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
A.
const [text, setText] = React.useState('');

<TextInput
  value={text}
  onChangeText={t => setText(t.toLowerCase())}
/>
B.
const [text, setText] = React.useState('');

<TextInput
  value={text.toUpperCase()}
  onChangeText={setText}
/>
C.
const [text, setText] = React.useState('');

<TextInput
  defaultValue={text.toUpperCase()}
  onChangeText={setText}
/>
D.
const [text, setText] = React.useState('');

<TextInput
  value={text}
  onChangeText={t => setText(t.toUpperCase())}
/>

Solution

  1. Step 1: Understand uppercase enforcement

    To force uppercase, transform input text to uppercase before saving state.
  2. Step 2: Check each option's logic

    const [text, setText] = React.useState('');
    
    <TextInput
      value={text}
      onChangeText={t => setText(t.toUpperCase())}
    />
    converts input to uppercase in onChangeText before calling setText.
    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}
    />
    uses defaultValue which is uncontrolled.
    const [text, setText] = React.useState('');
    
    <TextInput
      value={text}
      onChangeText={t => setText(t.toLowerCase())}
    />
    converts to lowercase, opposite of requirement.
  3. Final Answer:

    <TextInput value={text} onChangeText={t => setText(t.toUpperCase())} /> -> Option D
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard