I am trying to set a state variable using useState hook and then access(console.log) it inside a useEffect hook in a reactjs app. And also I displayed the value of state variable on front end. But the problem is that the state variable's value is displaying correctly on front end but undefined in console. Actually I want to set the state variable and access it inside the same useEffect hook. How to solve this?
const [localusername, setlocalusername] = useState();
const [localpassword, setlocalpassword] = useState();
useEffect(() => {
setlocalusername(localStorage.getItem("username"));
setlocalpassword(localStorage.getItem("password"));
console.log(localusername);
console.log(localpassword);
}, []);
Here is the attached snapshot of console...