In my React/JS application. this is part of the Component class I have:
<p>
<input
onChange={ this.handleEmailChange } value={ this.props.email } type='email'
className={ this.state.error ? 'form-control loginCredentialError shakeElement' : 'form-control' }
name='email'
placeholder='Email' required='True'
/>
<input
onChange={ this.handlePasswordChange } value={ this.props.password } type='password'
className={ this.state.error ? 'form-control loginCredentialError shakeElement' : 'form-control' } name='password'
placeholder='Password' required='True'
/>
</p>
<p>
<Button
onClick={ (event) => this.handleLoginClick(event) }
className='btn btn-round-lg btn-round btn-primary btn-block center-block'
>Sign In</Button>
</p>
As you can see I have handleEmailChange, handlePasswordChange, and handleLoginClick. Those three functions are of the same Component class as the render function
In this function, the this is evaluated as null, which makes this.setState fail.
handleEmailChange(event) {
const value = event.target.value;
console.log(value);
this.setState({ email: value });
}
however, in this function the this is fine and has a value. setState works well. Can anyone please let me know why the difference in these seemingly similar functions?
handleLoginClick(event) {
event.preventDefault();
const payload = {
'email': this.state.email,
'password': this.state.password,
};
this.setState({ communicating: true, error: false, errorServer: false });
...
...
...