react native get TextInput value

前端 未结 16 2028
一生所求
一生所求 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 16:43

    You should use States to store the value of input fields. https://facebook.github.io/react-native/docs/state.html

    • To update state values use setState

    onChangeText={(value) => this.setState({username: value})}

    • and get input value like this

    this.state.username

    Sample code

    export default class Login extends Component {
    
        state = {
            username: 'demo',
            password: 'demo'
        };
    
        User Name
         this.setState({username: value})}
            value={this.state.username}
        />
    
        Password
         this.setState({password: value})}
            value={this.state.password}
        />
    
        

提交回复
热议问题