Take this piece of code:
import React from 'react';
import { useState, useEffect } from 'react'
export function App() {
let [isAdmin, setAdmin] = useState(false)
const checkIfAdmin = async() => {
setAdmin(true)
}
useEffect(() => {
checkIfAdmin()
}, []);
console.log(isAdmin)
return (
<div className='App'>
<h1>test</h1>
</div>
);
}
When console logging isAdmin
, it comes out as false initially, but when checked again (such as in an onClick()
event), it comes out as true
. Why does it take 2 checks to finally output the desired result? How can I make it so that in checkIfAdmin
the changes immediately take place, and isAdmin
comes out as true
on the first time?