React Native TextInput: Handling User Input
TextInput is your app's keyboard entry point. Use it for search bars and forms, configuring features like auto-capitalization and keyboard type. Be careful with styling: Android adds a default underline, and multiline mode can break single-side border styles.
Why it exists
Apps need a standard way to get text from users. TextInput provides a unified, cross-platform API to handle keyboard input, which would otherwise require writing separate, complex native code for iOS and Android to manage keyboard interactions and text rendering.
The mental model
Treat TextInput as a controlled component. Your app's state is the single source of truth for the input's value. The user typing triggers an onChangeText callback to update your state, which then re-renders the component with the new value. This gives you complete control over the data flow.
How it works
You render a TextInput and subscribe to the onChangeText event. This event's callback function receives the new text every time the user types, which you use to update a state variable. That state variable is then passed back to the TextInput's value prop. You can configure its behavior with props like autoCapitalize, autoComplete, and keyboardType. For programmatic control, you can call methods like .focus() and .blur() on a reference to the component.
When to use it
Use it for any field where a user needs to type text. This includes login forms (username, password), search bars, messaging apps, and forms for user-generated content. The autoComplete prop is especially useful for improving the user experience in forms by suggesting saved information like names, emails, and addresses.
When not to use it
TextInput is for plain text. For rich text editing with complex styling (bold, italics, etc.), you'll need a more specialized third-party library. It is also not a button; for simple tap interactions, use a Pressable or Button component.
One canonical example
A common footgun is cross-platform styling. Android adds a default underline to TextInput. To create a consistent input with a full border, you must set underlineColorAndroid="transparent". Furthermore, if you set multiline={true}, single-side border styles (like borderBottomWidth) will not apply. The standard solution is to wrap your TextInput in a styled View component to achieve the desired border effect consistently on both platforms.
Interview question
What is the recommended approach for managing user input in a React Native TextInput?
- a.Provide an initial defaultValue prop and access the current input using a ref's getText() method.
- b.Allow the TextInput to manage its own internal state, retrieving the final value with a ref.
- c.Set the value prop to a state variable and update it via the onChangeText callback.Correct
- d.Directly modify the TextInput's content from a global store whenever the user types.
Why? this is the answer
The card emphasizes treating TextInput as a controlled component, where the app's state is the single source of truth, updated by onChangeText and reflected by the value prop. Option B describes an uncontrolled component, which is not the recommended mental model for TextInput.
Just read this? Test yourself on what you have been reading.
Read the original → reactnative.dev
- #react native
- #mobile
- #ui component
- #forms
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react native — each one lists the topics its interview covers.
See open roles