I've got a <TextInput>
and I gave it a ref
attribute by doing this:
ref={node => this.usernameInput = node;}
Then, I have a method when the user presses a <TouchableInput>
that tries to get the value of this.usernameInput
. this.usernameInput
is not null in my onPress
method, but I can't find a way to get the value of it! When I do this:
console.log(this.usernameInput.value);
It logs undefined
. If I set a breakpoint, and inspect this.usernameInput
I can see it, but there is no value
property or method, and I don't see any property or method that could return the current value. How do I get the value of my <TextInput>
?
EDIT
Here's my component class:
import {
View,
Text,
TextInput,
TouchableHighlight
} from 'react-native';
import {Manager as ModalManager} from 'react-native-root-modal';
class AppContainer extends React.Component {
loginModal;
constructor(props){
super(props);
this._onLoginPress = this._onLoginPress.bind(this);
this._createLoginModal = this._createLoginModal.bind(this);
}
async componentWillMount() {
this._createLoginModal();
}
render() {
return (
<View >
<Text>Hello.</Text>
</View>
);
}
}
_onLoginPress() {
//this returns 'undefined', although this.usernameInput is not undefined
console.log(`USERNAMEinputValue: ${this.usernameInput.props.value}`);
}
_createLoginModal() {
this.loginModal = new ModalManager(
<View >
<View >
<Text>Username:</Text>
<TextInput
placeholder="Username"
ref={node => {
this.usernameInput = node;
}}
></TextInput>
<TouchableHighlight
onPress={this._onLoginPress}
>
<Text>Login</Text>
</TouchableHighlight>
</View>
</View>
);
}
}