Handle the form submit
We have a component (container), that returns the form
import React, {Component} from 'react'
export default class SearchBar extends Component {
constructor(props) {
super(props);
this.state = {term: ''};
this.onInputChange = this.onInputChange.bind(this);
}
onInputChange(event) {
this.setState({term: event.target.value});
}
render() {
return (
<form className="input-group">
<input
placeholder="Get a five-day forecast in your favourite cities."
className="form-control"
value={this.state.term}
onChange={this.onInputChange}/>
<span className="input-group-btn">
<button type="submit" className="btn btn-secondary">Submit</button>
</span>
</form>
);
};
}
Forms are usually used to send something to the server. Now we do not control what happens, when the form is submitted, so let's start with assigning function to it's onSubmit attribute:
...
render() {
return (
<form onSubmit={this.onFormSubmit} className="input-group">
<input
placeholder="Get a five-day forecast in your favourite cities."
className="form-control"
value={this.state.term}
onChange={this.onInputChange}/>
<span className="input-group-btn">
<button type="submit" className="btn btn-secondary">Submit</button>
</span>
</form>
);
};
...
For now we should prevent form to submit anything to the server
...
onFormSubmit(event) {
event.preventDefault();
}
render() {
return (...