This is my code:
import React,{useEffect, useState} from 'react';
import bootstrap from '../../node_modules/Bootstrap/dist/css/bootstrap.min.css'
import '../ComponentStyles/NavbarStyle.css'
function Navbar() {
const [LUsername,setLUsername]=useState('')
const [LPassword,setLPassword]=useState('')
const [IsLogin,setIsLogin]=useState(false)
function Login(){
let credentials = JSON.parse(localStorage.getItem('cred'))
console.log(credentials)
for(let i=0;i<credentials.length;i++) {
if(credentials[i].username===LUsername && credentials[i].password===LPassword){
alert('in')
// why does IsLogin value is false even after setting it to true
setIsLogin(false)
alert(IsLogin)
}
}
if(IsLogin){
alert('login')
}
else{
alert('Login Failed')
}
On line 7 I have initialized Islogin variable with useState. On line 18 I am using the setIsLogin method to change it's initial value from false to true
but when I use alert to output it's value it gives me the initial value which is false. Also the value returned is true when I use conditional rendering in line 45, It works correct over there, Can someone tell me why the value is shown wrong in alert? Thanks.