React-Native TextInput
Syntax
The TextInput component in React-Native is used to render a field for user input.
import React, { useState } from 'react';
import { View, TextInput, Text } from 'react-native';
function LoginForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<View>
<TextInput
placeholder="Email"
value={email}
onChangeText={setEmail}
autoFocus
keyboardType="email-address"
autoCapitalize="none"
/>
<TextInput
placeholder="Password"
value={password}
onChangeText={setPassword}
secureTextEntry
/>
</View>
);
}
export default LoginForm;
Example
Here is an example of a simple login form that uses the TextInput component to handle user input:
import React, { useState } from 'react';
import { View, TextInput, Button } from 'react-native';
function LoginForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleLogin = () => {
// handle login logic
};
return (
<View>
<TextInput
placeholder="Email"
value={email}
onChangeText={setEmail}
autoFocus
keyboardType="email-address"
autoCapitalize="none"
/>
<TextInput
placeholder="Password"
value={password}
onChangeText={setPassword}
secureTextEntry
/>
<Button title="Login" onPress={handleLogin} />
</View>
);
}
export default LoginForm;
Output
The TextInput component will render an input field with the specified placeholder text and value. As the user types in the field, the onChangeText
function will be called with the updated value, which can be used to update state.
Explanation
The TextInput component is a core component in React-Native and is used to render input fields. It supports a variety of input types, including email, phone number, and numeric inputs. The onChangeText
function can be used to handle user input and update state as the user types.
Some common props used with TextInput include:
value
: The current value of the input fieldplaceholder
: Placeholder text to display in the input field when it is emptyautoFocus
: Whether the input field should be automatically focused when the component mountskeyboardType
: The type of keyboard to display to the user (e.g. numeric, email-address, phone-pad)autoCapitalize
: How to capitalize text input (e.g. "none", "words", "sentences")secureTextEntry
: Whether or not the text input should be hidden (for password input, for example)
Use
The TextInput component is a versatile component that can be used in a wide variety of forms and user input scenarios. It can be used to accept text input, numeric input, and even images or other file types with the appropriate props.
Important Points
- TextInput values should be stored in state since the component's render behavior is based on the state.
- TextInput's "editable" prop can be used to set whether the input field is editable or not, which can be useful for read-only fields or disabled states.
- TextInput's "onSubmitEditing" prop can be used to specify a function to call when the user submits the input (e.g. when they press the "enter" key).
Summary
The TextInput component in React-Native is a widely-used component for rendering input fields. It supports a variety of input types and props that can be used to customize its behavior. By managing value changes through state, the TextInput component can handle a wide range of user input scenarios.