-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When using React Lazy - recharts does not render properly #2566
Comments
I'm seeing the same thing, sometimes a few lines in a chart are drawn, but usually blank, and no error. Did you find a successful way to code-split this? |
May be related: #2736 (comment) |
+1 |
I can confirm the same. // Shortened
import {
// Rectangle,
Area,
AreaChart,
ReferenceLine,
// ResponsiveContainer,
Tooltip,
YAxis,
XAxis,
ReferenceArea,
Brush,
} from "recharts";
// Most recharts components don`t work if lazy loaded
const Rectangle = lazy(() => import("recharts").then((m) => ({ default: m.Rectangle })));
// const Area = lazy(() => import("recharts").then((m) => ({ default: m.Area })));
// const AreaChart = lazy(() => import("recharts").then((m) => ({ default: m.AreaChart })));
// const ReferenceLine = lazy(() => import("recharts").then((m) => ({ default: m.ReferenceLine })));
const ResponsiveContainer = lazy(() =>
import("recharts").then((m) => ({ default: m.ResponsiveContainer }))
);
// const Tooltip = lazy(() => import("recharts").then((m) => ({ default: m.Tooltip })));
// const YAxis = lazy(() => import("recharts").then((m) => ({ default: m.YAxis })));
// const XAxis = lazy(() => import("recharts").then((m) => ({ default: m.XAxis })));
// const ReferenceArea = lazy(() => import("recharts").then((m) => ({ default: m.ReferenceArea })));
// const Brush = lazy(() => import("recharts").then((m) => ({ default: m.Brush })));
...
return (
<Suspense fallback={progressCircle}>
<div style={{ width: "100%", height: 200 /*border: "dashed red"*/ }}>
<ResponsiveContainer width="100%" height="100%">
<AreaChart data={chartSliced} .../>
<YAxis .../>
<ReferenceLine .../>
<XAxis .../>
<Tooltip .../>
<Area .../>
<Brush .../>
</AreaChart>
</ResponsiveContainer>
</div>
</Suspense> |
Yep - it's probably due to how recharts generates the charts. The individual chart components are tightly coupled with their parent and every component is tightly coupled with the high level generator function. Probably without one or the other nothing can happen (just a theory) |
I guess this is rather hard to change, is it? When I build for production, the largest files are
The (internally) created |
It certainly would, but yeah incredibly hard to change right now. We're working on some refactoring so things are easier to understand and easier to move around. Changing this is changing how the library works though - much more than a refactor |
Sounds great, looking forward to it! |
Just letting you know what i did. Instead of cluttering my react app's dependencies and code with recharts, i use the standalone js distribution and whenever i need a chart i do this with an |
+1 lazy not works on component when i use fallback. It works on regular import. // Lazy load recharts components
const rechartsModule = import("recharts");
const LazyAreaChart = lazy(() => rechartsModule.then((module) => ({ default: module.AreaChart })));
const LazyArea = lazy(() => rechartsModule.then((module) => ({ default: module.Area as unknown as ComponentType<any> })));
const LazyXAxis = lazy(() => rechartsModule.then((module) => ({ default: module.XAxis as ComponentType<any> })));
const LazyYAxis = lazy(() => rechartsModule.then((module) => ({ default: module.YAxis as ComponentType<any> })));
const LazyCartesianGrid = lazy(() => rechartsModule.then((module) => ({ default: module.CartesianGrid as ComponentType<any> })));
const LazyResponsiveContainer = lazy(() => rechartsModule.then((module) => ({ default: module.ResponsiveContainer as ComponentType<any> })));
const LazyTooltip = lazy(() => rechartsModule.then((module) => ({ default: module.Tooltip as ComponentType<any> })));
const LazyReferenceLine = lazy(() => rechartsModule.then((module) => ({ default: module.ReferenceLine as ComponentType<any> })));
// ... (th Module lazy load testing code rechartsModule.then(module => {
console.log(module); // Check if the module is logged successfully
}); |
No updates other than this is still currently impossible for the same reasons. Any module that relies on |
Reproduction link
Steps to reproduce
See link, just uncomment the Lazy and comment the regular import
What is expected?
Rendering
What is actually happening?
Not rendering
The text was updated successfully, but these errors were encountered: