echarts-wordcloud icon indicating copy to clipboard operation
echarts-wordcloud copied to clipboard

ReferenceError: window is not defined on next.js

Open tiago138 opened this issue 2 years ago • 2 comments

Hi, I'm trying to use this chart on next.js but I'm getting the ReferenceError: window is not defined on next.js. I don't know what it can be since I'm already using other echarts charts on my project, I only get this erro, when I import echarts wordcloud. If anyone here know what can be causing the error I really would appreciate the help.

Thanks guys for your awesome project.

tiago138 avatar Sep 26 '22 18:09 tiago138

I get this on my console

error - node_modules/echarts-wordcloud/src/layout.js (12:0) @ eval
ReferenceError: window is not defined
null

tiago138 avatar Sep 26 '22 18:09 tiago138

I have faced the same issue recently, and I was able to resolve by importing echarts-wordcloud on the client side:

import {useEffect, useState} from "react";

import * as echarts from 'echarts/core';
import {CanvasRenderer} from "echarts/renderers";
import {
    TitleComponent,
} from 'echarts/components';
echarts.use([
    TitleComponent,
    CanvasRenderer
]);

export default function WordCloudChartClientComponent(props) {
   const [WordCloudInitialized, setWordCloudInitialized] = useState(false);
   useEffect(() => {
      // Initialize ECharts Word Cloud.
      const initWordCloud = async() => {
        await import('echarts-wordcloud');
        setWordCloudInitialized(true)
      }
      initWordCloud();
  }, []);

  return (
    <>
        WordCloudInitialized ?
             <SomeComponentDependentOnWordCloud echarts={echarts} />
            : <p>Loading...</p>
    </>
  );

}

achaitanyasai avatar May 21 '23 16:05 achaitanyasai