react native get TextInput value

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

    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 drawback!!!

    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}
      />
      ...
    }
    
    ...
    
    0 讨论(0)
  • 2020-12-04 16:48

    This work for me

        <Form>
    
        <TextInput
        style={{height: 40}}
        placeholder="userName"
        onChangeText={(text) => this.userName = text}
        />
    
        <TextInput
        style={{height: 40}}
        placeholder="Password"
        onChangeText={(text) => this.Password = text}
        />
    
    
        <Button 
        title="Sign in!" 
        onPress={this._signInAsync} 
        />
    
        </Form>
    

    and

      _signInAsync = async () => {
            console.log(this.userName)
            console.log(this.Password) 
      };
    
    0 讨论(0)
  • 2020-12-04 16:50

    Simply do it.

    this.state={f_name:""};
    
    textChangeHandler = async (key, val) => {
        await this.setState({ [key]: val });
    }
    
    <Textfield onChangeText={val => this.textChangeHandler('f_name', val)}>
    
    0 讨论(0)
  • 2020-12-04 16:50

    There is huge difference between onChange and onTextChange prop of <TextInput />. Don't be like me and use onTextChange which returns string and don't use onChange which returns full objects.

    I feel dumb for spending like 1 hour figuring out where is my value.

    0 讨论(0)
  • 2020-12-04 17:00

    If you set the text state, why not use that directly?

    _handlePress(event) {
      var username=this.state.text;
    

    Of course the variable naming could be more descriptive than 'text' but your call.

    0 讨论(0)
  • 2020-12-04 17:02
    constructor(props) {
            super(props);
    
            this.state ={
                commentMsg: ''         
            }
        }
    
     onPress = () => {
              alert("Hi " +this.state.commentMsg)
          }
    
     <View style={styles.sendCommentContainer}>
    
         <TextInput
            style={styles.textInput}
            multiline={true}
            onChangeText={(text) => this.setState({commentMsg: text})}
            placeholder ='Comment'/>
    
           <Button onPress={this.onPress} 
               title="OK!"
               color="#841584"
            />
    
      </TouchableOpacity>
    
    </View>
    
    0 讨论(0)
提交回复
热议问题