Thuta Learning
React Native
IntermediateMobile DevelopmentIntermediate

Handling User Input

React NativeLesson 920 min

What you'll walk away with

  • Explain the React and native-platform behavior of Handling User Input
  • Test accessibility, failure, and lifecycle cases
  • Write maintainable, testable, and performant React Native

Explain it simply

Handling User Input, in plain language

TextInput is the component for entering text (similar to HTML's <input>) — pairing the value prop with the onChangeText callback via useState makes it a controlled input (state updates on every keystroke). Button is a basic built-in component, but its styling is limited — for custom designs, it's common to build with TouchableOpacity/Pressable combined with View+Text.

Problem → Why → Solution

The problem — why this concept matters

Assuming onChangeText is onChange (web-style) and using the wrong prop name — in React Native it must be onChangeText

A common mistake

Leaving TextInput uncontrolled without pairing value/onChangeText — it won't stay in sync with state

Core concept — understand how it works

For Handling User Input, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.

Mental model
  1. JavaScript state
  2. native component and layout
  3. Android or iOS UI

Explain the React and native-platform behavior of Handling User Input

Core syntax or pattern

javascript
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';

export default function LoginForm() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  return (
    <View style={styles.form}>
      <TextInput
        style={styles.input}
        placeholder="Username"
        value={username}
        onChangeText={setUsername}
      />
      <TextInput
        style={styles.input}
        placeholder="Password"
        secureTextEntry
        value={password}
        onChangeText={setPassword}
      />
      <TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
        <Text style={styles.buttonText}>Login</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  form: { padding: 16 },
  input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
  button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
  buttonText: { color: 'white', fontWeight: '600' },
});

For Handling User Input, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.

Read the code step by step

  1. JavaScript stateimport { useState } from 'react';

    TextInput is the component for entering text (similar to HTML's <input>) — pairing the value prop with the onChangeText callback via useState makes it a controlled input (state updates on every keystroke). Button is a basic built-in component, but its styling is limited — for custom designs, it's common to build with TouchableOpacity/Pressable combined with View+Text.

  2. native component and layoutsecureTextEntry

    For Handling User Input, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.

  3. Android or iOS UI});

    Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Execution flow
  1. JavaScript state
  2. native component and layout
  3. Android or iOS UI

Success

Explain the React and native-platform behavior of Handling User Input

Failure

Assuming onChangeText is onChange (web-style) and using the wrong prop name — in React Native it must be onChangeText

Let's think about this for a moment

TextInput is the component for entering text (similar to HTML's <input>) — pairing the value prop with the onChangeText callback via useState makes it a controlled input (state updates on every keystroke). Button is a basic built-in component, but its styling is limited — for custom designs, it's common to build with TouchableOpacity/Pressable combined with View+Text.

Let's connect this to a real scenario

To build a login form, you need two TextInputs for username/password and one submit button — adding secureTextEntry={true} to the password field masks it as dots (••••). Using TouchableOpacity instead of Button lets you set a custom color/border/padding — Button only uses the platform's default style, which makes it hard to customize.

Code Example

javascript
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';

export default function LoginForm() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  return (
    <View style={styles.form}>
      <TextInput
        style={styles.input}
        placeholder="Username"
        value={username}
        onChangeText={setUsername}
      />
      <TextInput
        style={styles.input}
        placeholder="Password"
        secureTextEntry
        value={password}
        onChangeText={setPassword}
      />
      <TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
        <Text style={styles.buttonText}>Login</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  form: { padding: 16 },
  input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
  button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
  buttonText: { color: 'white', fontWeight: '600' },
});
You should see
On the login form, you can type a username/password, and pressing the Login button prints the username to the console.

5-Minute Try-It

Build a login form yourself — include 2 TextInputs (username, password) and one TouchableOpacity button.

A Quick Heads-Up

Don't assume that adding secureTextEntry to a password TextInput makes the password 'secure' — it's only visual masking; actual transmission/storage security needs to be handled separately on the backend.

Real-world usage

How to apply it in a real project

Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Mini challenge

Build it yourself

Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
javascript
import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, StyleSheet } from 'react-native';

export default function LoginForm() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  return (
    <View style={styles.form}>
      <TextInput
        style={styles.input}
        placeholder="Username"
        value={username}
        onChangeText={setUsername}
      />
      <TextInput
        style={styles.input}
        placeholder="Password"
        secureTextEntry
        value={password}
        onChangeText={setPassword}
      />
      <TouchableOpacity style={styles.button} onPress={() => console.log(username)}>
        <Text style={styles.buttonText}>Login</Text>
      </TouchableOpacity>
    </View>
  );
}

const styles = StyleSheet.create({
  form: { padding: 16 },
  input: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, marginBottom: 12 },
  button: { backgroundColor: '#3b82f6', padding: 14, borderRadius: 8, alignItems: 'center' },
  buttonText: { color: 'white', fontWeight: '600' },
});

Expected: Explain the React and native-platform behavior of Handling User Input

Show hint

Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

Quick quiz 1 / 3

What best describes the core idea of Handling User Input?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • JavaScript state → native component and layout → Android or iOS UI
  • For Handling User Input, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
  • Assuming onChangeText is onChange (web-style) and using the wrong prop name — in React Native it must be onChangeText
  • Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

React Native Documentation — React Native

Easy traps

  • Assuming onChangeText is onChange (web-style) and using the wrong prop name — in React Native it must be onChangeText
  • Leaving TextInput uncontrolled without pairing value/onChangeText — it won't stay in sync with state
  • Assuming one simulator's happy path proves every real-device, release-build, permission, accessibility, offline, and lifecycle path.

Hands-on Exercise

Test an original Handling User Input example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.

You'll know it worked when: On the login form, you can type a username/password, and pressing the Login button prints the username to the console.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Handling User Input | Thuta Learning