React Native - Difference between onChangeText and onSubmitEditing?
Solution 1
onSubmitEditing
is triggered when you click the text input submit button (keyboard button).
onChangeText
is triggered when you type any symbol in the text input.
For example, you might need some validation on every key press, in that case you will use onChangeText
, if you need the validation to trigger when you finish typing, you need onSubmitEditing
In your example, you will achieve what you need in both cases.
Solution 2
onSubmitEditing
is a callback when you tap the button in the screenshot below.
onChangeText
is a callback when you type anything into a TextInput
.
Solution 3
onSubmitEditing
: When you want to submit the editing of text field and want to call some action like dimissing the mobile keyboard or calling submit action or API to pass current screen data,one can use it.
In short,when you are done with adding the text to field and want to proceed with some action to next Screen,one can use it.
It called only when one press the keyboard button. e.g. When we press GO,RETURN,Search button on keyboard.
2: onChangeText
onChangeText
: Its typical use to update the state of Component with TextInput value like Reactjs onChange
event.
Its called on every change of character.
Related videos on Youtube
m1771vw
Updated on June 19, 2022Comments
-
m1771vw almost 2 years
From the Facebook React Native Text Input documentation, I was able to discern that this is what happens when
onSubmitEditing
is used:Callback that is called when the text input's submit button is pressed.
However, there wasn't anything for
onChangeText
. I'm assuming if the text has changed, then it will trigger.Why would I want to use one other than the other? For example if I'm making something that takes in text for the
TextInput
field, wouldn't I just want to useonChangeText
? In some examples I've seen them useonSubmitEditing
and I'm confused on why you would use one over the other. This question is different than wondering how to make the submit button - I'm asking why I would useonChangeText
vs.onSubmitEditing
.-
m1771vw about 6 years@RIYAJKHAN I'm not sure how my question is a duplicate of the suggested one when I'm asking for the difference between
onChangeText
vs.onSubmitEditing
, whereas that thread asks how to create a submit button foronSubmitEditing
.
-