I am having a problem which needs your helps
I have a changeProfile
function which will be executed after onClick
in /editpage
const appContext = useContext(AppContext);
const [userLogin, setUserLogin] = appContext.userLogin;
const [userProfile, setUserProfile] = useState<UserProfile>({
name: userLogin.name,
....
});
const changeProfile = e => {
e.preventDefault();
post(API)
.then(() => {
setUserLogin({
...userLogin,
name: userProfile.name,
});
router.push('/mypage');
})
.catch(error => {
setEditError(error[0]);
window.scrollTo(0, 0);
});
};
context.tsx
const [userLogin, setUserLogin] = useState({
email: '',
name: '',
....
});
after page redirects to /mypage
, console logs warning error as title and it only logs warning at the first time (if I back to /editpage
and changeProfile
one more time, console logs nothing in/mypage
)
I have tried to redirect
after setUserLogin
done as code below but it's not working
const changeProfile = e => {
e.preventDefault();
post(API)
.then(() => {
setUserLogin({
...userLogin,
name: userProfile.name,
});
})
.catch(error => {
setEditError(error[0]);
window.scrollTo(0, 0);
});
};
const firstUpdate = useRef(true);
useLayoutEffect(() => {
if (firstUpdate.current) {
firstUpdate.current = false;
return;
}
console.log(userLogin); //already updated
router.push('/mypage');
}, [userLogin]);
Thanks for reading!
PS: Problem can be solved with
setTimeout(() => {
router.push('/mypage');
}, 1000);
but its absolutely not a right choice