This warning occurs when React.lazy() receives the same component value instead of a promise from a dynamic import. It typically happens when the lazy function is not properly structured to return an import() statement. Understanding the correct lazy() syntax and avoiding duplicate imports prevents this warning and ensures code-splitting works as intended.
React.lazy() expects its callback function to return a Promise from a dynamic import() call that resolves to a module with a default export. When the lazy function resolves to the same value (particularly when it returns the component itself instead of importing it), React detects that the dynamic import is not actually loading a separate chunk and issues a warning. This indicates that code-splitting is not working as intended, and the component will not be split into a separate bundle. The warning is React's way of alerting developers that they are not using lazy loading correctly, which defeats the purpose of code-splitting for performance optimization.
React.lazy() requires a callback function that returns a Promise from a dynamic import() call. The most common mistake is passing a component directly or using a regular import statement.
// WRONG - static import, then lazy() wrapping the component
import MyComponent from "./MyComponent";
const LazyComponent = React.lazy(() => MyComponent);
// WRONG - returning the component directly
const LazyComponent = React.lazy(() => {
return MyComponent;
});
// CORRECT - lazy() wraps a dynamic import() call
const LazyComponent = React.lazy(() => import("./MyComponent"));
// CORRECT - with explicit return
const LazyComponent = React.lazy(() => {
return import("./MyComponent");
});If you are using a static import at the top of the file and then wrapping it in lazy(), move the import into the lazy() callback as a dynamic import. Remove any top-level import of the component.
// BEFORE
import MyComponent from "./MyComponent";
const LazyComponent = React.lazy(() => MyComponent);
// AFTER
const LazyComponent = React.lazy(() => import("./MyComponent"));Ensure the callback function in lazy() returns the Promise from import() directly, not a function that needs to be executed or a resolved value. The import() call itself returns a Promise.
// WRONG - returning a function that imports
const LazyComponent = React.lazy(() => {
const importComponent = () => import("./MyComponent");
return importComponent();
});
// CORRECT - returning the Promise directly
const LazyComponent = React.lazy(() => import("./MyComponent"));Define lazy components outside of component functions, at the top level of the module. This ensures they are created once and reused, not recreated on every render. Creating lazy components inside render functions can cause unnecessary re-renders and defeat code-splitting benefits.
// CORRECT - declared at module level
const LazyDashboard = React.lazy(() => import("./Dashboard"));
const LazySettings = React.lazy(() => import("./Settings"));
function App() {
return (
<Suspense fallback={<Loading />}>
<LazyDashboard />
</Suspense>
);
}
// WRONG - recreated on every render
function App() {
const LazyDashboard = React.lazy(() => import("./Dashboard"));
return (
<Suspense fallback={<Loading />}>
<LazyDashboard />
</Suspense>
);
}Always wrap lazy components in a Suspense boundary to show a fallback while the component is loading. This is required for lazy() to work properly and gives the best user experience.
import { Suspense, lazy } from "react";
const LazyComponent = lazy(() => import("./MyComponent"));
function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
);
}If the component you are trying to lazy-load imports the parent component, it creates a circular dependency that prevents dynamic loading. Refactor your code to remove circular imports by extracting shared code into a separate module that both components can import from.
// WRONG - circular dependency
// App.tsx imports LazyComponent
// LazyComponent imports App
const LazyComponent = lazy(() => import("./LazyComponent")); // Won't code-split
// CORRECT - extract shared logic
// utils.ts - contains shared code
// App.tsx imports utils
// LazyComponent imports utils (not App)
const LazyComponent = lazy(() => import("./LazyComponent")); // Code-splits correctlyBuild your application in production mode and inspect the output to confirm code-splitting is working. Use build analysis tools to verify that lazy-loaded components are in separate chunks.
# For a typical React setup
npm run build
# For Next.js
npm run build
# Check the build output for separate chunk files for each lazy component
# You should see separate .js files for lazy-loaded componentsThe "lazy() resolving to the same value" warning appears because React detects that the callback function did not actually return a Promise from a dynamic import - meaning no code-splitting can occur. This is purely a developer warning and does not break your application, but it indicates suboptimal code organization. React.lazy() is specifically designed to work with module bundlers like Webpack or Vite that support code-splitting of dynamic imports. If you are using a different bundler or setup, verify that it supports dynamic import() syntax. In Next.js, you can use the next/dynamic function instead of React.lazy() for better integration with Next.js routing and data fetching. Remember that lazy components must export a default export from their module - if a component uses named exports, create an intermediate module that re-exports it as the default. The Suspense boundary used with lazy() will only show its fallback UI while the chunk is being downloaded and parsed; once loaded, the component renders immediately without any loading delay on subsequent visits if the chunk is already cached by the browser.
React Hook useCallback has a missing dependency: 'variable'. Either include it or remove the dependency array react-hooks/exhaustive-deps
React Hook useCallback has a missing dependency
Cannot use private fields in class components without TS support
Cannot use private fields in class components without TS support
Cannot destructure property 'xxx' of 'undefined'
Cannot destructure property of undefined when accessing props
useNavigate() may be used only in the context of a <Router> component.
useNavigate() may be used only in the context of a Router component
Cannot find module or its corresponding type declarations
How to fix "Cannot find module or type declarations" in Vite