Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native get TextInput value

Tags:

react-native

I am stuck with a very simple problem. I have login form with username, password and button. In my button handler, I try to get the textinput value. But always get undefined value. Am I missing something?

render() {         <ExScreen           headerColor={this.state.headerColor}           scrollEnabled={this.state.enableScroll}           style={styles.container} >           <View >             <View  >               <View style={[styles.inputContainer]} >                 <TextInput                   ref= "username"                   onChangeText={(text) => this.setState({text})}                   value={this.state.username}                 />               </View>  <Button style={{color: 'white', marginTop: 30, borderWidth: 1, borderColor: 'white', marginLeft: 20*vw, marginRight: 20*vw, height: 40, padding: 10}}               onPress={this._handlePress.bind(this)}>               Sign In             </Button>    ...  _handlePress(event) {     var username=this.refs.username.value; 
like image 555
WayneZhao Avatar asked Oct 02 '15 18:10

WayneZhao


People also ask

What is TextInput value?

The value attribute specifies the value of an <input> element. The value attribute is used differently for different input types: For "button", "reset", and "submit" - it defines the text on the button. For "text", "password", and "hidden" - it defines the initial (default) value of the input field.


1 Answers

The quick and less optimized way to do this is by using arrow function inside your onChangeText callback, by passing username as your argument in your onChangeText callback.

<TextInput     ref= {(el) => { this.username = el; }}     onChangeText={(username) => this.setState({username})}     value={this.state.username} /> 

then in your _handlePress method

_handlePress(event) {     let username=this.state.username; } 

But this has several drawbacks!!!

  1. On every render of this component a new arrow function is created.
  2. If the child component is a PureComponent it will force re-renders unnecessarily, this causes huge performance issue especially when dealing with large lists, table, or component iterated over large numbers. More on this in React Docs

Best practice is to use a handler like handleInputChange and bind ```this`` in the constructor.

... constructor(props) {   super(props);   this.handleChange= this.handleChange.bind(this); }  ... handleChange(event = {}) {   const name = event.target && event.target.name;   const value = event.target && event.target.value;    this.setState([name]: value); } ...  render() {   ...   <TextInput     name="username"     onChangeText={this.handleChange}     value={this.state.username}   />   ... }  ... 

Or if you are using es6 class property shorthand which autobinds this. But this has drawbacks, when it comes to testing and performance. Read More Here

... handleChange= (event = {}) => {   const name = event.target && event.target.name;   const value = event.target && event.target.value;    this.setState([name]: value); } ...  render() {   ...   <TextInput     name="username"     onChangeText={this.handleChange}     value={this.state.username}   />   ... }  ... 
like image 182
ferndopolis Avatar answered Nov 01 '22 18:11

ferndopolis