Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear React Native TextInput

Working through the Redux AddTodo example in React Native. The first AddTodo example below uses state to store the TextInput value and works fine.

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}) } } />         );     } } 

However following a few of the Redux examples, the following code is much shorter and also works except that the TextInput value is not cleared after submitting

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

Is there any way I can clear the InputText value from onSubmitEditing?

like image 432
Dave Pile Avatar asked Jul 22 '17 01:07

Dave Pile


People also ask

How do I delete multiple TextInput in React Native?

var App = React. createClass({ clearText(fieldName) { this. refs[fieldName]. setNativeProps({text: ''}); }, render() { return ( <View style={styles.

How do you remove focus from TextInput in React Native?

To unfocus a TextInput in React Native, we can use the Keyboard. dismiss method. to set the onSubmitEditing prop to Keyboard. dismiss .

What is Clearbuttonmode in React Native?

In this post, we will create one react native project with one text input field and one clear button. The clear button will remove all text from the input field.


2 Answers

Add ref to your TextInput, for example:

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

then call this.textInput.clear() to clear your input value

like image 196
Kawatare267 Avatar answered Oct 10 '22 03:10

Kawatare267


For iOS, it will give the default clear text button.

<TextInput clearButtonMode="always" /> 

See the doc

like image 27
Aseem Avatar answered Oct 10 '22 02:10

Aseem