react native get TextInput value

前端 未结 16 2035
一生所求
一生所求 2020-12-04 16:10

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

16条回答
  •  一生所求
    2020-12-04 17:02

    This piece of code worked for me. What I was missing was I was not passing 'this' in button action:

     onPress={this._handlePress.bind(this)}>
    --------------
    
      _handlePress(event) {
    console.log('Pressed!');
    
     var username = this.state.username;
     var password = this.state.password;
    
     console.log(username);
     console.log(password);
    }
    
      render() {
        return (
          
    
           {
              this.setState({username:text});
            }}
          onSubmitEditing={(event) => {
         this.refs.psw.focus();
    
          }}
          />
    
           {
              this.setState({password:text});
            }}
          />
    
          
    
          
        );``
      }
    }
    

提交回复
热议问题