In React Native, the onSubmit event is commonly used with form elements like TextInput, but unlike HTML, React Native does not have a <form> element. So, the equivalent of an onSubmit event typically comes from:
1. TextInput's onSubmitEditing prop
This is the closest native equivalent to an onSubmit in React Native, used when a user presses the "return" or "enter" key on the keyboard.
import React, { useState } from 'react';
import { View, TextInput, Button, Alert } from 'react-native';
const App = () => {
const [inputValue, setInputValue] = useState('');
const handleSubmit = () => {
Alert.alert('Submitted Value:', inputValue);
};
return (
<View style={{ padding: 20 }}>
<TextInput
placeholder="Enter text"
value={inputValue}
onChangeText={setInputValue}
onSubmitEditing={handleSubmit} // Triggered when pressing Enter
returnKeyType="done"
style={{ borderWidth: 1, marginBottom: 10, padding: 8 }}
/>
<Button title="Submit" onPress={handleSubmit} />
</View>
);
};
export default App;