setState is not updating the value of isCurrectLogin.
import React, { Component } from 'react';
import UserRecords from './UserRecords.json'
the json file has following records:
[
{
"id" : 1,
"userName" : "Bala",
"password" : "123"
},
{
"id" : 1,
"userName" : "John",
"password" : "456"
},
{
"id" : 1,
"userName" : "Ram",
"password" : "789"
},
{
"id" : 1,
"userName" : "Peter",
"password" : "1011"
}
]
And i extract class component.
export default class UserLogin extends Component {
constructor(){
super();
this.state = {
currentUser : "",
currentPassword : "",
isCurrectLogin : false,
}
}
loginClickHandler = () => {
UserRecords.map((d)=>{
for(var i = 0; i < UserRecords.length; i++){
if(UserRecords[i].userName == "Bala"){
this.setState({isCurrectLogin : true})
} else {
this.setState({isCurrectLogin : false})
}
}
})
if (this.state.isCurrectLogin) {
alert("user name is correct")
} else {
alert("User name is wrong")
}
}
render(){
return(
<div>
<Button onClick = {this.loginClickHandler}> Login </Button>
</div>
)
}
}
Please help me where I the code I missed.
Thank you very much.
