This must be a simple question that I have not been able to find the answer to:
The below code works in React-native for Web, as you would expect in React.js... but it does NOT work for in iOS
import React, {useState} from 'react';
import {TextInput} from 'react-native';
const App = (props) =>{
const [input, setInput] = useState('');
const onChangeText (text) => {
setInput(text)
}
const onSubmitted (event) => {
console.log(event.target.value) // <== this does not work (is undefined)
}
...
//more code here
...
return (
<TextInput
style={styles.input}
onChangeText={onChangeText}
onSubmitEditing={onSubmitted}
value={input}
/>
);
};
export default App;
I've tried option #2 - mimicking examples for how the onChangeEvent should work/be implemented:
const onSubmitted (text) => {
console.log(text) // <== this does not work EITHER (is undefined)
}
OK, after lots and lots of googling...
I found that the correct variable that is passed from the
onSubmittedEditingevent is not aneventobject and not even a simpletextstring as in the case of theonChangeTextevent. But rather it's avalueobject with anativeEventproperty. This object of typenativeEventin turn contains atextproperty/variable which contains the actual text/value from theTextInputfield/control.Now if you're developing for the web, you'll see that this
nativeEventhas a whole bunch of properties including the familiar web/HTMLtargetproperty, which then contains avalueproperty ==> BUT, THIS MIGHT CONFUSE YOU AS IT DID ME ==> because on other platforms (I can only speak definitively right now about ios) thevalueobject has only 3 properties and corresponding values (the below is an example if you/the user entered "3" as the value in thetextInputcontrol:so that makes the following the correct syntax for your react-native code: