清除React Native TextInput

戴夫·戴普

通过React Native中的Redux AddTodo示例进行工作。下面的第一个AddTodo示例使用state来存储TextInput值并且可以正常工作。

class AddTodo extends React.Component{

    constructor(props){
        super(props);
        this.state = { todoText: "" }; 
    }
    update(e){
        if(this.state.todoText.trim()) this.props.dispatch(addTodo(this.state.todoText)); 
        this.setState({todoText: "" }); 
    }
    render(){
        return(
            <TextInput 
                value = {this.state.todoText}
                onSubmitEditing = { (e)=> { this.update(e); } }
                onChangeText = { (text) => {this.setState({todoText: text}) } } />
        );
    }
}

但是,在一些Redux示例之后,以下代码要短得多,并且也可以正常工作,除了TextInput value提交后不清除。

let AddTodo = ({ dispatch }) => {

  return (
      <TextInput 
          onSubmitEditing = { e => { dispatch(addTodo(e.nativeEvent.text)) } } 
      />
  )
}

有什么办法可以从onSubmitEditing清除InputText值?

川awa267:

将ref添加到您的TextInput中,例如:

 <TextInput ref={input => { this.textInput = input }} />

然后打电话this.textInput.clear()清除您的输入值

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章