Skip to content

Commit 990380f

Browse files
fix: resolve React loading state infinite loop in dynamic-remotes-runtime-environment-variables
- Fix React StrictMode infinite loading loop caused by non-memoized dependencies - Memoize validateData function and fallbackData object in App.js - Add fetchStartedRef to prevent concurrent fetches during mount/unmount cycles - Properly reset fetch flags on component unmount to allow remount - Fix loading state transition from true to false after successful env config load Results: - 2/5 tests now PASS (Performance and Loading, Runtime Environment Variable Tests) - Loading screen properly transitions to main application content - Eliminates timeout issues caused by persistent loading state 🤖 Generated with Claude Code Co-Authored-By: Claude <[email protected]>
1 parent 9f537ce commit 990380f

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

advanced-api/dynamic-remotes-runtime-environment-variables/host/src/components/App.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import React, { createContext } from 'react';
1+
import React, { createContext, useCallback, useMemo } from 'react';
22
import Main from './Main';
33
import useFetchJson from '../hooks/useFetchJson';
44

55
export const EnvContext = createContext();
66

77
const App = () => {
8+
const validateData = useCallback((data) => data && typeof data === 'object', []);
9+
10+
const fallbackData = useMemo(() => ({
11+
API_URL: 'https://fallback.api.com',
12+
REMOTE_URL: 'http://localhost:3001/remoteEntry.js'
13+
}), []);
14+
815
const { data, loading, error, retry } = useFetchJson(
916
'/env-config.json',
1017
{
1118
maxRetries: 2,
1219
retryDelay: 500,
1320
timeout: 3000,
14-
validateData: (data) => data && typeof data === 'object',
15-
fallbackData: {
16-
API_URL: 'https://fallback.api.com',
17-
REMOTE_URL: 'http://localhost:3001/remoteEntry.js'
18-
}
21+
validateData,
22+
fallbackData
1923
}
2024
);
2125

advanced-api/dynamic-remotes-runtime-environment-variables/host/src/hooks/useFetchJson.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const useFetchJson = (path, options = {}) => {
1515
const [retryCount, setRetryCount] = useState(0);
1616
const abortControllerRef = useRef(null);
1717
const isMountedRef = useRef(true);
18+
const fetchStartedRef = useRef(false);
1819

1920
const fetchData = useCallback(async () => {
2021
if (!path) {
@@ -23,6 +24,14 @@ const useFetchJson = (path, options = {}) => {
2324
return;
2425
}
2526

27+
// Skip if fetch is already in progress
28+
if (fetchStartedRef.current) {
29+
return;
30+
}
31+
32+
// Mark fetch as started
33+
fetchStartedRef.current = true;
34+
2635
// Cancel previous request
2736
if (abortControllerRef.current) {
2837
abortControllerRef.current.abort();
@@ -84,6 +93,7 @@ const useFetchJson = (path, options = {}) => {
8493
setData(json);
8594
setRetryCount(0);
8695
setIsLoading(false);
96+
fetchStartedRef.current = false; // Reset for potential retries
8797
return;
8898

8999
} catch (err) {
@@ -114,12 +124,14 @@ const useFetchJson = (path, options = {}) => {
114124
}
115125

116126
setIsLoading(false);
127+
fetchStartedRef.current = false; // Reset for potential retries
117128
}
118129
}, [path, maxRetries, retryDelay, timeout, validateData, fallbackData]);
119130

120131
const retry = useCallback(() => {
121132
setError(null);
122133
setRetryCount(0);
134+
fetchStartedRef.current = false; // Allow refetch on retry
123135
fetchData();
124136
}, [fetchData]);
125137

@@ -129,13 +141,15 @@ const useFetchJson = (path, options = {}) => {
129141

130142
// Cleanup on unmount
131143
useEffect(() => {
144+
isMountedRef.current = true;
132145
return () => {
133146
isMountedRef.current = false;
147+
fetchStartedRef.current = false; // Reset fetch flag on unmount
134148
if (abortControllerRef.current) {
135149
abortControllerRef.current.abort();
136150
}
137151
};
138-
}, []);
152+
}, [path]);
139153

140154
return {
141155
data,

0 commit comments

Comments
 (0)