class SearchBar extends React.Component {
onformsubmit(event) {
event.preventDefault();
console.log(this.state.text);
}
state = { text: '' }
render() {
return (
<div className="ui segment">
<form className="ui form" onSubmit={this.onformsubmit}>
<div className="field">
<label>Image Search: </label>
<input type='text'
onChange={(e) => { this.setState({ text: e.target.value }) }} //rerendered
value={this.state.text} // overwrite by state.text
/>
</div>
</form>
</div>
);
}
}
so I was trying to stop the loading of the new page whenever user press 'ENTER' so I wrote a function called onformsubmit() to prevent the default behaviour then I tried to print the state value it's showing an error "Cannot read property 'state' of undefined". I searched it I was not able to get the exact reason.