Summary
We're encountering a React context identity issue in the Next.js dashboard app when using hooks from the @kubernetesjs/react package. These hooks use TanStack Query (@tanstack/react-query) under the hood, but in the context of Next.js (especially with SSR), React treats the context reference as different — even when a QueryClientProvider is present at the top level of the app.
This results in useQuery() or useMutation() calls inside the imported hooks acting as if there's no provider, causing errors or undefined results.
💡 Temporary Fix
The working workaround is to explicitly pass context: defaultContext in the options of every hook usage:
import { defaultContext } from '@tanstack/react-query';
import { useReadCoreV1NamespacedConfigMapQuery } from '@kubernetesjs/react';
const result = useReadCoreV1NamespacedConfigMapQuery({
path: { namespace: 'default', name: 'my-config' },
options: {
context: defaultContext,
enabled: true,
},
});
This ensures the hook uses the same QueryClient context as the one provided by the app.
🤯 Root Cause
React (and TanStack Query) rely on reference identity for context propagation. If the QueryClientProvider and the internal hook reference don’t come from the same in-memory Context object, React treats them as separate — even if the versions match.
In this case:
- The app provides a
QueryClientProvider with defaultContext.
- The hook, compiled into
@kubernetesjs/react, may reference a different Context (e.g. due to bundling or module boundary).
- Result:
useQuery sees no provider.
This is especially problematic in SSR and Next.js environments where module scoping, ESM/CJS boundaries, or Webpack deduping can misalign.
🛠 Proposed Solutions
-
Short-term: Keep using context: defaultContext for all hook usages.
-
Package fix (in @kubernetesjs/react):
-
Ensure @tanstack/react-query is a peer dependency in @kubernetesjs/react, not a bundled dependency:
"peerDependencies": {
"@tanstack/react-query": "^5.x"
}
-
Enforce module resolution in the dashboard app’s next.config.js:
// next.config.js
webpack: (config) => {
config.resolve.alias['@tanstack/react-query'] = require.resolve(
'next/node_modules/@tanstack/react-query'
);
return config;
};
✅ Action Items
Summary
We're encountering a React context identity issue in the Next.js dashboard app when using hooks from the
@kubernetesjs/reactpackage. These hooks use TanStack Query (@tanstack/react-query) under the hood, but in the context of Next.js (especially with SSR), React treats the context reference as different — even when aQueryClientProvideris present at the top level of the app.This results in
useQuery()oruseMutation()calls inside the imported hooks acting as if there's no provider, causing errors or undefined results.💡 Temporary Fix
The working workaround is to explicitly pass
context: defaultContextin theoptionsof every hook usage:This ensures the hook uses the same
QueryClientcontext as the one provided by the app.🤯 Root Cause
React (and TanStack Query) rely on reference identity for context propagation. If the
QueryClientProviderand the internal hook reference don’t come from the same in-memoryContextobject, React treats them as separate — even if the versions match.In this case:
QueryClientProviderwithdefaultContext.@kubernetesjs/react, may reference a differentContext(e.g. due to bundling or module boundary).useQuerysees no provider.This is especially problematic in SSR and Next.js environments where module scoping, ESM/CJS boundaries, or Webpack deduping can misalign.
🛠 Proposed Solutions
Short-term: Keep using
context: defaultContextfor all hook usages.Package fix (in
@kubernetesjs/react):Export
defaultContextfrom the package to ensure unified usage.(Optional) Wrap all generated hooks using a helper like
withDefaultContext(hook)to apply it automatically.Ensure
@tanstack/react-queryis a peer dependency in@kubernetesjs/react, not a bundled dependency:Enforce module resolution in the dashboard app’s
next.config.js:✅ Action Items
context: defaultContextto all uses of hooks from@kubernetesjs/react@kubernetesjs/reactfor re-usable context