I have a method to save employee. Here I want to set the employee state with the returned saved object. It is updating the state value but not showing next line log after setEmployee(). But it is showing updated value on useEffect() on employee change. So why it is not showning after setting the value on next line although I have used await.
My code is below.
Initial state, an empty object:
const [employee, setEmployee] = useState({});
Use effect on employee:
useEffect(() => {
console.log(">>>> inEmpolyeeUseEffect: " + employee);
}, [employee]);
Add employee method:
const addEmployee = async (empObj) => {
try {
const response = await axios.post(`http://localhost:9000/employee/save`, { ...empObj });
const data = response.data;
await setEmployee(JSON.stringify(data));
console.log("EmployeeDataAfterSave: " + employee); // log empty object here like {}
...
} catch (error) {
console.log(error +" :: " + JSON.stringify(error));
}
}