I am new to react and just started with event handling but while writing a simple onSubmit function I am getting the error (Cannot read property 'state' of undefined).
I know it can be corrected by
onSubmitFunct = event =>{}
but why doesn't
`onSubmitFunct (event) {}`
work?
I just invoked a function of a class app using .this
then how is it undefined
?
Why did I get undefined
error when I am accessing the function of the same class using .this
in the render function?
import React from 'react';
import ReactDom from 'react-dom';
class APP extends React.Component {
state = { term: '' };
onSubmitFunct(event) {
event.preventDefault();
console.log(this.state.term);
}
render() {
return (
<div className="frm">
<form onSubmit={this.onSubmitFunct}> //why this doen't work
<div className='infrm'>
<label>Enter Text </label>
<input type="text" value={this.state.term} onChange={e =>
this.setState({ term: e.target.value })} />
</div>
</form>
</div>
);
}
}
ReactDom.render(<APP />, document.querySelector('#root'));