diff --git a/src/common/test/ProfileContextFetcher.tsx b/src/common/test/ProfileContextFetcher.tsx index dbd17510..50059d15 100644 --- a/src/common/test/ProfileContextFetcher.tsx +++ b/src/common/test/ProfileContextFetcher.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useState } from 'react'; +import React, { useContext, useEffect } from 'react'; import { ProfileContext } from '../../profile/context/ProfileContext'; @@ -7,12 +7,11 @@ function ProfileContextFetcher({ }: { children: React.ReactElement | React.ReactNodeArray; }): React.ReactElement { - const [fetchStarted, setFetchStarted] = useState(false); const { data, fetch } = useContext(ProfileContext); - if (!fetchStarted) { + + useEffect(() => { fetch(); - setFetchStarted(true); - } + }); if (!data) { return
; diff --git a/src/profile/components/profile/Profile.tsx b/src/profile/components/profile/Profile.tsx index 18999f8c..338223d2 100644 --- a/src/profile/components/profile/Profile.tsx +++ b/src/profile/components/profile/Profile.tsx @@ -69,9 +69,13 @@ function Profile(): React.ReactElement { const isProfileFound = !!(data && data.myProfile); const failedToFetchUserProfileData = tunnistamoUser && !isProfileFound && !!error; - if (isProfileFound && !isProfileInitialized) { - fetchProfile(); - } + + useEffect(() => { + if (isProfileFound && !isProfileInitialized) { + fetchProfile(); + } + }, [fetchProfile, isProfileFound, isProfileInitialized]); + const isLoadingProfile = isProfileFound && !isProfileComplete; const getPageTitle = () => {