Skip to content

Commit

Permalink
(HP-894) Fix React warning
Browse files Browse the repository at this point in the history
New kind of warning started to show up after Apollo client update: https://fb.me/setstate-in-render

Child components caused parent component to re-render while parent was still rendering.

Moved the triggering functions inside  "useEffect" to delay them until rendering is done.
  • Loading branch information
NikoHelle committed Dec 10, 2021
1 parent 4f5e482 commit 2918602
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/common/test/ProfileContextFetcher.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState } from 'react';
import React, { useContext, useEffect } from 'react';

import { ProfileContext } from '../../profile/context/ProfileContext';

Expand All @@ -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 <div data-testid="no-data-fetched"></div>;
Expand Down
10 changes: 7 additions & 3 deletions src/profile/components/profile/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit 2918602

Please sign in to comment.