I'm presented with a rather silly problem. I am in the process of creating my first React application and I have encountered a little issue, where I am not able to clear my input value, after I submit a form. A tried googling this problem, found some similar threads here, but I was not able to resolve this. I do NOT want to change the state of my component/application, just to change the value of the input to an empty string. I tried clearing the value of the input in my onHandleSubmit()
function, but I got an error:
"Cannot set property 'value' of undefined".
My SearchBar Component:
import React, { Component } from "react"; class SearchBar extends Component { constructor(props) { super(props); this.state = { city: "" }; this.onHandleChange = this.onHandleChange.bind(this); this.onHandleSubmit = this.onHandleSubmit.bind(this); } render() { return ( <form> <input id="mainInput" onChange={this.onHandleChange} placeholder="Get current weather..." value={this.state.city} type="text" /> <button onClick={this.onHandleSubmit} type="submit"> Search! </button> </form> ); } onHandleChange(e) { this.setState({ city: e.target.value }); } onHandleSubmit(e) { e.preventDefault(); const city = this.state.city; this.props.onSearchTermChange(city); this.mainInput.value = ""; } } export default SearchBar;
To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.
To clear React Native TextInput, we can set the value with a state and clear the state. to set the value prop to val . And we set onChangeText to setVal to set val to the inputted value. Next, we add a Button with the onPress prop set to a function that set val to an empty string with setVal .
You are having a controlled component where input
value is determined by this.state.city
. So once you submit you have to clear your state which will clear your input automatically.
onHandleSubmit(e) { e.preventDefault(); const city = this.state.city; this.props.onSearchTermChange(city); this.setState({ city: '' }); }
Since you input field is a controlled element, you cannot directly change the input field value without modifying the state.
Also in
onHandleSubmit(e) { e.preventDefault(); const city = this.state.city; this.props.onSearchTermChange(city); this.mainInput.value = ""; }
this.mainInput
doesn't refer the input since mainInput is an id
, you need to specify a ref to the input
<input ref={(ref) => this.mainInput= ref} onChange={this.onHandleChange} placeholder="Get current weather..." value={this.state.city} type="text" />
In you current state the best way is to clear the state to clear the input value
onHandleSubmit(e) { e.preventDefault(); const city = this.state.city; this.props.onSearchTermChange(city); this.setState({city: ""}); }
However if you still for some reason want to keep the value in state even if the form is submitted, you would rather make the input uncontrolled
<input id="mainInput" onChange={this.onHandleChange} placeholder="Get current weather..." type="text" />
and update the value in state onChange
and onSubmit
clear the input using ref
onHandleChange(e) { this.setState({ city: e.target.value }); } onHandleSubmit(e) { e.preventDefault(); const city = this.state.city; this.props.onSearchTermChange(city); this.mainInput.value = ""; }
Having Said that, I don't see any point in keeping the state unchanged, so the first option should be the way to go.
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