apexcharts / react-apexcharts

📊 React Component for ApexCharts
https://apexcharts.com
MIT License
1.32k stars 159 forks source link

ApexChart Not working with NextJs 13 latest #526

Closed soaphyls closed 3 months ago

soaphyls commented 1 year ago

Hi, Can you check why apexchart throws multiple errors with nextJs 13. Server Error ReferenceError: window is not defined This error happened while generating the page. Any console logs will be displayed in the terminal window. even after using import dynamic from 'next/dynamic'

const Chart = dynamic(() => import('react-apexcharts'), { ssr: false });

arome commented 1 year ago

Instead of importing react-apexcharts dynamically, create a component and import react-apexcharts normally in that component then import that component dynamically into your parent component. Here's an example of how it should look like

Parent.tsx

import dynamic from "next/dynamic";

const DynamicGraph = dynamic(() => import("@/components/Child"), {
  ssr: false,
});

const Parent = () => {
    return <><DynamicGraph /></>
}

export default Parent;

Child.tsx

import Chart from "react-apexcharts";

const Child = () => {
     return <><Chart options={options} series={series} /></>
}

export default Child;
david-vendel commented 1 year ago

Is there a better/nicer solution than adding another component and doing props drilling?

mvandergrift commented 1 year ago

I'm not sure this any nicer, but it keeps things in a single component:

  const [ReactApexChart, setChart] = useState(null);

  useEffect(() => {
    if (typeof window !== "undefined") {
      setChart(() => require("react-apexcharts").default);
    }
  }, []);

  return ReactApexChart && <ReactApexChart  />;
};
shakilahmedriyad commented 1 year ago

Next dynamic works for me though. I'm using Next v13.2.4 and react-apexcharts v1.4.1 i'm using appDir

import dynamic from 'next/dynamic';
const Chart = dynamic(() => import('react-apexcharts'), {
  ssr: false,
});

This should be a better way

Dmarcotrigiano commented 12 months ago

Instead of importing react-apexcharts dynamically, create a component and import react-apexcharts normally in that component then import that component dynamically into your parent component. Here's an example of how it should look like

Parent.tsx

import dynamic from "next/dynamic";

const DynamicGraph = dynamic(() => import("@/components/Child"), {
  ssr: false,
});

const Parent = () => {
    return <><DynamicGraph /></>
}

export default Parent;

Child.tsx

import Chart from "react-apexcharts";

const Child = () => {
     return <><Chart options={options} series={series} /></>
}

export default Child;

Dynamically importing the charts alone was not the solution. This was the solution, what a pain.

longlostnick commented 7 months ago

React.lazy works for me:

export const ApexChart = lazy(() => import('react-apexcharts'));
github-actions[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.