Reaction prevents form submission when a button is clicked
I have the following React Search Bar component where the parent container can call using
<SearchBar onInputChange={this.handleInputChange} />
Each time the user changes the input, the parent container will be notified. That's why there is no submit button in my search bar.
However, I find that if I press the enter button inside my search bar, the whole page refreshes. And I do not want this.
I know that if I have a button in the form, I can call event.preventDefault (). But in this case I donโt have a button, so I donโt know what to do here.
class SearchBar extends Component { constructor(props) { super(props); this.state = { value: '' }; this.handleChange = this.handleChange.bind(this) } handleChange(e) { this.setState({ value: e.target.value }); this.props.onInputChange(e.target.value); } render() { return ( <div id="search-bar"> <form> <FormGroup controlId="formBasicText"> <FormControl type="text" onChange={this.handleChange} value={this.state.value} placeholder="Enter Character Name" /> </FormGroup> </form> </div> ); } } export default SearchBar
source share