From 2df8f4f145149e587f7e7db9bde083c27de89e09 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Tue, 1 Nov 2022 18:22:32 +0530 Subject: [PATCH] Fix profile page data reset on re-focus of window (#5311) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- apps/web/pages/settings/my-account/profile.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/web/pages/settings/my-account/profile.tsx b/apps/web/pages/settings/my-account/profile.tsx index d68cd985a1..115a1a7231 100644 --- a/apps/web/pages/settings/my-account/profile.tsx +++ b/apps/web/pages/settings/my-account/profile.tsx @@ -158,9 +158,12 @@ const ProfileView = () => { }>(); const { reset } = formMethods; - + const formInitializedRef = useRef(false); useEffect(() => { - if (user) + // The purpose of reset is to set the initial value obtained from tRPC. + // `user` would change for many reasons (e.g. when viewer.me automatically fetches on window re-focus(a react query feature)) + if (user && !formInitializedRef.current) { + formInitializedRef.current = true; reset({ avatar: user?.avatar || "", username: user?.username || "", @@ -168,6 +171,7 @@ const ProfileView = () => { email: user?.email || "", bio: user?.bio || "", }); + } }, [reset, user]); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion