Steps:
Use
ref
to Control Focus
Assign aref
to eachTextInput
to programmatically control focus.Handle
onSubmitEditing
Use theonSubmitEditing
event to focus the next input.Set
returnKeyType
SetreturnKeyType
to"next"
for intermediate fields and"done"
for the last one.Prevent Keyboard Dismissal
UseblurOnSubmit={false}
to keep the keyboard open while navigating.
Code Example:
import React, { useRef } from 'react';
import { TextInput, View, StyleSheet } from 'react-native';
const App = () => {
const input1Ref = useRef(null);
const input2Ref = useRef(null);
const input3Ref = useRef(null);
return (
<View style={styles.container}>
<TextInput
ref={input1Ref}
style={styles.input}
placeholder="Input 1"
returnKeyType="next"
onSubmitEditing={() => input2Ref.current?.focus()}
blurOnSubmit={false}
/>
<TextInput
ref={input2Ref}
style={styles.input}
placeholder="Input 2"
returnKeyType="next"
onSubmitEditing={() => input3Ref.current?.focus()}
blurOnSubmit={false}
/>
<TextInput
ref={input3Ref}
style={styles.input}
placeholder="Input 3"
returnKeyType="done"
onSubmitEditing={() => console.log('Form submitted')}
/>
</View>
);
};
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', padding: 16 },
input: { height: 50, borderColor: 'gray', borderWidth: 1, marginBottom: 10, paddingHorizontal: 10 },
});
export default App;
Key Properties:
-
ref
: Links eachTextInput
to a reference for focus control. -
onSubmitEditing
: Triggers focus on the next field when the "Next" button is pressed. -
returnKeyType
: Sets the keyboard button type to"next"
or"done"
. -
blurOnSubmit
: Prevents the keyboard from closing when moving to the next input.
Top comments (0)