I have this weird scenario where when i relog with different account in my project it renders 3 times previous account's data and then it renders current account's data as expected. This causes some unnecessary network calls and many other bugs. I have been examining this problem and this is the only function where I set userData
.
why this useEffect:
useEffect(() => {
if (userData?.status === 500) {
setItemToLocalStorage("token", "")
history.push("/login")
}
let updatedState = {...state}
console.log(`${updatedState?.isAdmin} before`)
if (userData && userDataFetched) {
updatedState = {
...updatedState,
isAdmin: userData?.isQappAdmin,
userRole: userData?.roles[0],
userName: userData?.userName
}
if(userData?.roles[0] !== IRoles.WASHER_MANAGER) {
updatedState = {
...updatedState,
user: userData
}
}
}
console.log(`${updatedState?.isAdmin} after`)
setState(updatedState)
}, [userData])
logs:
false before
true after
true before
false after
?