I am getting error "Window is not defined while pushing the code to the deployment or build
Answered
Citrine Wagtail posted this in #help-forum
Citrine WagtailOP
"use client";
import React, { useState } from "react";
import ReactApexChart from "react-apexcharts";
interface DonutPieChartProps {}
interface DonutPieChartState {
series: number[];
options: {
chart: {
type: string;
};
labels: string[];
responsive: {
breakpoint: number;
options: {
chart: {
width: number;
};
legend: {
position: string;
};
};
}[];
legend: {
show: boolean;
};
dataLabels: {
enabled: boolean;
};
plotOptions: {
pie: {
startAngle: number;
endAngle: number;
donut: {
size: string;
};
};
};
};
}
const DonutPieChart: React.FC<DonutPieChartProps> = (props) => {
const [state, setState] = useState<DonutPieChartState>({
series: [44, 55, 13, 43, 22],
options: {
chart: {
type: "donut",
},
labels: ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"],
responsive: [
{
breakpoint: 480,
options: {
chart: {
width: 200,
},
legend: {
position: "bottom",
},
},
},
],
legend: {
show: false,
},
dataLabels: {
enabled: false,
},
plotOptions: {
pie: {
startAngle: -90,
endAngle: 90,
donut: {
size: "50%", // Set the size of the donut to only half
},
},
},
},
});
return (
<div>
<div id="chart">
<ReactApexChart
options={{
...state.options,
chart: {
...state.options.chart,
type: "donut",
},
}}
series={state.series}
type="donut"
/>
</div>
</div>
);
};
export default DonutPieChart;Here is the code and the problem is it doesn't show any error in dev
Answered by joulev
https://nextjs-faq.com/browser-api-client-component
your
your
react-apexcharts library is not server-side render-able so you need to use next/dynamic on it with ssr: false17 Replies
Citrine WagtailOP
Up!
@Citrine Wagtail
"use client";
import React, { useState } from "react";
import ReactApexChart from "react-apexcharts";
interface DonutPieChartProps {}
interface DonutPieChartState {
series: number[];
options: {
chart: {
type: string;
};
labels: string[];
responsive: {
breakpoint: number;
options: {
chart: {
width: number;
};
legend: {
position: string;
};
};
}[];
legend: {
show: boolean;
};
dataLabels: {
enabled: boolean;
};
plotOptions: {
pie: {
startAngle: number;
endAngle: number;
donut: {
size: string;
};
};
};
};
}
const DonutPieChart: React.FC<DonutPieChartProps> = (props) => {
const [state, setState] = useState<DonutPieChartState>({
series: [44, 55, 13, 43, 22],
options: {
chart: {
type: "donut",
},
labels: ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"],
responsive: [
{
breakpoint: 480,
options: {
chart: {
width: 200,
},
legend: {
position: "bottom",
},
},
},
],
legend: {
show: false,
},
dataLabels: {
enabled: false,
},
plotOptions: {
pie: {
startAngle: -90,
endAngle: 90,
donut: {
size: "50%", // Set the size of the donut to only half
},
},
},
},
});
return (
<div>
<div id="chart">
<ReactApexChart
options={{
...state.options,
chart: {
...state.options.chart,
type: "donut",
},
}}
series={state.series}
type="donut"
/>
</div>
</div>
);
};
export default DonutPieChart;
Here is the code and the problem is it doesn't show any error in dev
https://nextjs-faq.com/browser-api-client-component
your
your
react-apexcharts library is not server-side render-able so you need to use next/dynamic on it with ssr: falseAnswer
@joulev https://nextjs-faq.com/browser-api-client-component
your `react-apexcharts` library is not server-side render-able so you need to use `next/dynamic` on it with `ssr: false`
Citrine WagtailOP
can you please tell me the implementation of that using my code ?
@joulev check https://github.com/apexcharts/react-apexcharts/issues/526 or https://github.com/apexcharts/react-apexcharts/issues/469
Citrine WagtailOP
now i am getting this error
@Citrine Wagtail now i am getting this error
it doesnt look related to the window error
what fix are you using in the above two github issue links i gave you?
@joulev what fix are you using in the above two github issue links i gave you?
Citrine WagtailOP
i g the error is from the state i created but somehow its gone now
@joulev what fix are you using in the above two github issue links i gave you?
Citrine WagtailOP
it works now i used the next/dynamic in the useEffect
thanks for the help
no, you use either next/dynamic or useEffect
no need of both
@joulev no, you use either next/dynamic or useEffect
Citrine WagtailOP
useEffect(() => {
const loadApexChart = async () => {
const dynamicApexChart = await import("react-apexcharts");
setApexChart(() => dynamicApexChart);
};
loadApexChart();
}, []);yes that works
Citrine WagtailOP
yes so should i use it or not ?
@Citrine Wagtail yes so should i use it or not ?
Well in the above, you are not using next/dynamic right? Yes your code works well, no need to change anything
@joulev Well in the above, you are not using next/dynamic right? Yes your code works well, no need to change anything
Citrine WagtailOP
yep yep so am only using useEffect i was wrong about what i mentioned earlier