Skip to main content

TextInput

A text field. It renders an <input type="text">, or a <textarea> with multiline.

import { useState } from 'react'
import { TextInput } from 'react-native'

export function EmailField() {
const [email, setEmail] = useState('')
return (
<TextInput
value={email}
onChangeText={setEmail}
placeholder="you@example.com"
keyboardType="email-address"
autoComplete="email"
autoCapitalize="none"
/>
)
}

The field has no border, no padding, and no background, as on iOS. Its text direction follows the text you type.

Multiline​

With multiline, the field grows with its text. numberOfLines is the minimum number of rows. rows does the same, and wins over numberOfLines, as in React Native. A height in the style fixes the size.

Enter​

FieldEnter does
Single-lineCalls onSubmitEditing, then blurs the field. The browser's default stays, so a field inside a <form> submits the form.
MultilineAdds a new line. With submitBehavior set to submit or blurAndSubmit, it calls onSubmitEditing instead. Shift+Enter always adds a new line.

submitBehavior and blurOnSubmit work as on native.

A tap outside the field​

A tap outside a focused field keeps the focus in the field, so the keyboard stays, as on native. The tap still reaches what it lands on, so a Pressable fires. Inside a ScrollView, its keyboardShouldPersistTaps decides. A tap on another text field moves the focus there.

This applies to a touch or a pen tap. A mouse click moves the focus as on any web page.

Zoom in Safari on iOS​

Safari on iOS zooms in on a focused field when its text is smaller than 16 px. The page stays zoomed after the blur. A TextInput with no fontSize uses 14, as on native, so Safari zooms in on it.

To stop the zoom, give each field a fontSize of 16 or more:

<TextInput style={{ fontSize: 16 }} />

Don't add maximum-scale=1 to the viewport meta tag. It stops this zoom, but Chrome on Android then blocks pinch zoom too, and some users need pinch zoom to read.

Props​

PropOn the web
value, defaultValue, onChangeText, onChangeAs on native. A value with no handler stays fixed, with no React warning.
placeholder, maxLength, autoFocusAs on native.
secureTextEntrytype="password".
keyboardTypeBecomes inputmode, so a phone shows the matching keyboard. The input type stays text, so the browser adds no validation.
returnKeyTypeBecomes enterkeyhint, the label of the Enter key on a phone keyboard.
inputMode, enterKeyHintWin over keyboardType and returnKeyType when you set them.
showSoftInputOnFocusfalse is inputmode="none", so a phone shows no keyboard.
autoComplete, textContentTypeThe HTML autocomplete tokens. The React Native-only values map, for example name-given to given-name and sms-otp to one-time-code. textContentType applies when autoComplete isn't set.
autoCapitalize, autoCorrect, spellCheckThe matching HTML attributes.
editable, readOnlyeditable={false} is readonly, not disabled, so the field stays focusable and its text selectable.
placeholderTextColorThe color of the placeholder.
selectionColor, cursorColor, caretHiddenThe background of selected text and the color of the caret. caretHidden makes the caret transparent.
selection, onSelectionChangeAs on native. onSelectionChange also fires when the caret moves.
selectTextOnFocus, clearTextOnFocus, contextMenuHiddenAs on native.
textAlignAs on native.
onFocus, onBlur, onEndEditing, onKeyPress, onSubmitEditing, onContentSizeChangenativeEvent is the DOM event, with text, eventCount, key, selection, or contentSize added. nativeEvent.target is the element. onEndEditing fires on blur.
onPress, onPressIn, onPressOutA press with a pointer, as on native. onPress then focuses the field, unless editable is false. A key in the field never presses.
allowFontScaling, maxFontSizeMultiplierAs on Text. See Font scaling.
onLayout, accessibility props, nativeID, testIDAs on View.
The responder propsDo nothing: the press of the field sets them, as on native.

These native-only props are accepted and ignored: clearButtonMode, keyboardAppearance, textAlignVertical, underlineColorAndroid, passwordRules, dataDetectorTypes, and the other iOS and Android props.

Ref​

The ref is the <input> or <textarea> element, with clear(), isFocused(), getNativeRef(), and setNativeProps({ text, style }) added. It also has the measure methods of View.

TextInput.State has currentlyFocusedInput(), focusTextInput(element), and blurTextInput(element).