React-native输入框如何绑定清除数据

1、定义input接入输入框输入内容,定义绑定的方法

constructor(props) {
    super(props);
    this.state = {
      input: '',
    };
    //下面两条语句将两个回调函数和成员方法绑定
    this.handleInput = this.handleInput.bind(this);
  }


//处理输入的消息
  handleInput(text) {
    this.setState({input: text});
  }

2、编写TextInput

<TextInput
  style={styles.inputView}
  placeholder="请在此输入留言内容"
  placeholderTextColor={'#b3b3b3'}
  value={this.state.input}
  onChangeText={newText => this.handleInput(newText)}></TextInput>

这里重点是

value={this.state.input}

 这样TextInput的输入框就和this.state.input绑定了,要情况TextInput输入框,只需要进行以下操作即可

this.setState({input: ''});

只需要以上两步,即可完成TextInput跟this.state.input的绑定,从而对TextInput输入框进行清空 

猜你喜欢

转载自blog.csdn.net/chali1314/article/details/126530414