Skip to content

Commit b269dc3

Browse files
vchaindzclaude
andcommitted
fix: Implement dynamic React Query loading to resolve createContext error
- Create QueryProvider component with dynamic React Query import - Replace static imports with async loading to avoid bundling issues - Add fallback handling when React Query fails to load - Ensure React is available globally before loading React Query - Update both AppRouter and AppWithLoader to use new QueryProvider This should resolve the persistent "Cannot read properties of undefined (reading 'createContext')" error by loading React Query after ensuring React context is properly available. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 5b9c8a3 commit b269dc3

File tree

7 files changed

+201
-134
lines changed

7 files changed

+201
-134
lines changed

package-lock.json

Lines changed: 116 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/data/database.jsonic

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

public/data/database.jsonic.gz

0 Bytes
Binary file not shown.

src/components/AppRouter.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
import { useState, useEffect } from 'react';
22
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
3-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
4-
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
3+
import QueryProvider from './QueryProvider';
54
import Dashboard from './Dashboard';
65
import JsonicBenchmark from './JsonicBenchmark';
76
import LoadingOverlay from './LoadingOverlay';
87
import { setMigrationProgressCallback } from '../services/api-jsonic';
98
import { performSimpleMigration } from '../services/simpleMigration';
109

11-
const queryClient = new QueryClient({
12-
defaultOptions: {
13-
queries: {
14-
refetchOnWindowFocus: false,
15-
retry: 1,
16-
staleTime: 30000, // 30 seconds
17-
},
18-
},
19-
});
20-
2110
interface MigrationProgress {
2211
phase: 'checking' | 'loading' | 'migrating' | 'complete' | 'error' | 'idle';
2312
current: number;
@@ -119,7 +108,7 @@ export default function AppRouter() {
119108
const basename = import.meta.env.BASE_URL || '/';
120109

121110
return (
122-
<QueryClientProvider client={queryClient}>
111+
<QueryProvider>
123112
<Router basename={basename}>
124113
{!isReady && !currentPath.includes('/jsonic-bench') && (
125114
<LoadingOverlay
@@ -132,9 +121,7 @@ export default function AppRouter() {
132121
<Route path="/" element={isReady ? <Dashboard /> : null} />
133122
<Route path="/jsonic-bench" element={<JsonicBenchmark />} />
134123
</Routes>
135-
136-
<ReactQueryDevtools initialIsOpen={false} />
137124
</Router>
138-
</QueryClientProvider>
125+
</QueryProvider>
139126
);
140127
}

src/components/AppWithLoader.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
import { useState, useEffect } from 'react';
2-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
3-
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
2+
import QueryProvider from './QueryProvider';
43
import Dashboard from './Dashboard';
54
import LoadingOverlay from './LoadingOverlay';
65
import { setMigrationProgressCallback } from '../services/api-jsonic';
76
import { checkAndMigrateWorker } from '../services/workerMigration';
87

9-
const queryClient = new QueryClient({
10-
defaultOptions: {
11-
queries: {
12-
refetchOnWindowFocus: false,
13-
retry: 1,
14-
staleTime: 30000, // 30 seconds
15-
},
16-
},
17-
});
18-
198
interface MigrationProgress {
209
phase: 'checking' | 'loading' | 'migrating' | 'complete' | 'error' | 'idle';
2110
current: number;
@@ -106,15 +95,14 @@ export default function AppWithLoader() {
10695
}, []);
10796

10897
return (
109-
<QueryClientProvider client={queryClient}>
98+
<QueryProvider>
11099
{!isReady && (
111100
<LoadingOverlay
112101
{...migrationProgress}
113102
onComplete={() => setIsReady(true)}
114103
/>
115104
)}
116105
{isReady && <Dashboard />}
117-
<ReactQueryDevtools initialIsOpen={false} />
118-
</QueryClientProvider>
106+
</QueryProvider>
119107
);
120108
}

src/components/QueryProvider.tsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React, { useState, useEffect, type ReactNode } from 'react';
2+
3+
interface QueryProviderProps {
4+
children: ReactNode;
5+
}
6+
7+
let QueryClient: any = null;
8+
let QueryClientProvider: any = null;
9+
let ReactQueryDevtools: any = null;
10+
11+
export default function QueryProvider({ children }: QueryProviderProps) {
12+
const [isLoaded, setIsLoaded] = useState(false);
13+
const [queryClient, setQueryClient] = useState<any>(null);
14+
15+
useEffect(() => {
16+
const loadReactQuery = async () => {
17+
try {
18+
// Ensure React is available before importing
19+
if (typeof React === 'undefined') {
20+
console.error('React is not available');
21+
return;
22+
}
23+
24+
// Dynamic import to avoid bundling issues
25+
const [reactQuery, reactQueryDevtools] = await Promise.all([
26+
import('@tanstack/react-query'),
27+
import('@tanstack/react-query-devtools')
28+
]);
29+
30+
QueryClient = reactQuery.QueryClient;
31+
QueryClientProvider = reactQuery.QueryClientProvider;
32+
ReactQueryDevtools = reactQueryDevtools.ReactQueryDevtools;
33+
34+
const client = new QueryClient({
35+
defaultOptions: {
36+
queries: {
37+
refetchOnWindowFocus: false,
38+
retry: 1,
39+
staleTime: 30000,
40+
},
41+
},
42+
});
43+
44+
setQueryClient(client);
45+
setIsLoaded(true);
46+
} catch (error) {
47+
console.error('Failed to load React Query:', error);
48+
// Fallback - just render children without query provider
49+
setIsLoaded(true);
50+
}
51+
};
52+
53+
loadReactQuery();
54+
}, []);
55+
56+
if (!isLoaded) {
57+
return <div>Loading...</div>;
58+
}
59+
60+
// If React Query failed to load, just render children
61+
if (!QueryClient || !QueryClientProvider || !queryClient) {
62+
console.warn('React Query not available, running without query capabilities');
63+
return <>{children}</>;
64+
}
65+
66+
return (
67+
<QueryClientProvider client={queryClient}>
68+
{children}
69+
{import.meta.env.DEV && ReactQueryDevtools && <ReactQueryDevtools />}
70+
</QueryClientProvider>
71+
);
72+
}

src/main.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { StrictMode } from 'react'
1+
import React, { StrictMode } from 'react'
22
import { createRoot } from 'react-dom/client'
3+
4+
// Ensure React is available globally for libraries that need it
5+
if (typeof window !== 'undefined') {
6+
(window as any).React = React;
7+
}
38
import './index.css'
49
import App from './App.tsx'
510

0 commit comments

Comments
 (0)