window is undefined in next js when using materialize css

混江龙づ霸主 提交于 2021-02-11 17:16:51

问题


I'm using next js. When I try to import M from 'materialize-css'; I get window is undefined.


回答1:


You can only import the materialize-css on client side by using dynamic.

import dynamic from 'next/dynamic';
const M = dynamic(() => import('materialize-css'), {
    ssr: false,
});



回答2:


I added a useEffect and added this line:

     if(typeof window !== 'undefined'){
       const M = require('materialize-css');
       ...
     }
      

instead of the import statement



来源:https://stackoverflow.com/questions/63292172/window-is-undefined-in-next-js-when-using-materialize-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!