TextInput is React Native's <input type="text">, and the controlled-input pattern you know from React on the web works with it. The text arrives through onChangeText, and whether your state also drives what the field shows depends on which of value or defaultValue you pass. That choice starts to matter as soon as your own code changes the text, for example to empty a comment box after posting.

Key terms

  • onChangeText: the callback TextInput calls every time the text changes, with the new text as a single string.
  • value: the text to show, and once it is set, the native text is forced to match it, which makes the TextInput a controlled component.
  • defaultValue: an initial value that changes as soon as the user starts typing.
  • maxLength: a number that limits how many characters can be entered, enforced inside the native text field.

How value drives the field

Whenever value is a string that differs from the last text the native field reported, TextInput sends the new string down to the native view. That is why calling setText('') empties a controlled field: the empty string differs from what the user typed, so it replaces that text on screen.

Typing doesn't trigger that push. After each change, TextInput records the typed text as the last native text, so a value that only echoes what the user typed matches it and nothing is sent back.

If you pass both props, value wins. TextInput gives the native view value as its text and falls back to defaultValue only when value is not a string.

What onChangeText hands you

onChangeText receives the new text as its only argument. It is typed (text: string) => unknown, so a state setter fits as it is, and onChangeText={setText} needs no wrapper function.

TextInput also accepts onChange. On each native change event it calls onChange with the whole event first, then calls onChangeText with only the string it read from event.nativeEvent.text. When the text is all you need, onChangeText saves you from unpacking the event.

Starting from defaultValue

defaultValue gives the field its first text, and from then on the field shows what the user types. Pair it with onChangeText and your state still gets a copy of every edit:

tsx
<TextInput
  placeholder="Display name"
  defaultValue={user.displayName}
  onChangeText={setDisplayName}
/>

Nothing in this field sets its text again after it opens on the saved name. That fits a profile form where you only read the result when the user saves. When your code has to change the text after the first render, by clearing it or filling in a suggestion, pass value, because it is the prop that forces the native text to match your state.

maxLength stops typing in the native field

You could cap the length in JavaScript, either by trimming in the handler with text.slice(0, 280) or by rejecting an edit and leaving value unchanged. Keeping value the same to block an edit can make the field flicker. maxLength avoids that: iOS and Android both enforce the cap inside the native text field, so length logic stays out of onChangeText entirely.

To stop editing altogether, for example while a form is being sent, set editable={false}. It also blocks edits without flicker.

Clearing a comment after posting

CommentBox, the code example below the lesson, puts these props on one field. value={comment} makes the field and the {comment.length} / {MAX_COMMENT} counter read the same state, and maxLength={MAX_COMMENT} stops the field at 280 characters. The Post handler calls onPost(comment) and then setComment(''). Because the field is controlled, that empty string reaches the native view and clears the text, and the counter goes back to 0 on the next render.

Code Examples

tsx
import {useState} from 'react';
import {Button, Text, TextInput, View} from 'react-native';

const MAX_COMMENT = 280;

type CommentBoxProps = {
  onPost: (comment: string) => void;
};

export function CommentBox({onPost}: CommentBoxProps) {
  const [comment, setComment] = useState('');

  return (
    <View style={{padding: 16, gap: 8}}>
      <TextInput
        placeholder="Add a comment"
        value={comment}
        onChangeText={setComment}
        maxLength={MAX_COMMENT}
        style={{borderWidth: 1, padding: 8}}
      />
      <Text>{comment.length} / {MAX_COMMENT}</Text>
      <Button
        title="Post"
        onPress={() => {
          onPost(comment);
          setComment('');
        }}
      />
    </View>
  );
}
✓ Completed