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?
var App = React. createClass({ clearText(fieldName) { this. refs[fieldName]. setNativeProps({text: ''}); }, render() { return ( <View style={styles.
To unfocus a TextInput in React Native, we can use the Keyboard. dismiss method. to set the onSubmitEditing prop to Keyboard. dismiss .
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.
Add ref to your TextInput, for example:
<TextInput ref={input => { this.textInput = input }} />
then call this.textInput.clear()
to clear your input value
For iOS, it will give the default clear text button.
<TextInput clearButtonMode="always" />
See the doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With