I will get error accessing the state inside onFormSubmit because when onFormSubmit was called 'this' was lost. I am not able to find out what happened in render function that made it loose the 'this' reference to our component class.
- I know I can resolve this by using arrow function as they take the 'this' from its surrounding environment or lexically.
- Can also be resolved by using bind().
class SearchBar extends React.Component{
state = {term: 'Hi There'};
onFormSubmit(e){
console.log(this.state.term)
}
render(){
return (
<div className="ui segment">
<form onSubmit={this.onFormSubmit} className="ui form">
<label>Image Search</label>
<input type="text" value={this.state.term} onChange={(e) => {
this.setState({term: e.target.value})
}}/>
</form>
</div>
)
}
}
export default SearchBar;