0

众所周知NextJS,正在使用服务器端渲染,我想使用SurveyJs它,但surveyJS正在使用一些必须在客户端执行的功能。在我的情况下,我想使用StylesManager.applyTheme,但它会引发服务器错误ReferenceError: document is not defined

我可以使用任何可能的方式applyTheme在客户端执行该功能吗?

4

2 回答 2

1

您可以通过创建一个使用外部函数来应用此主题的 useEffect Hook 来实现此目的:

import {useEffect} from 'react'

useEffect(() => {
  const newSurveyCreator = new SurveyJSCreator.SurveyCreator('surveyCreatorContainer', surveyCreatorConfig);
  
  newSurveyCreator.saveSurveyFunc = function() {
    console.log(this);
    console.log(this && this.text);
  };
    
  updateSurveyCreator(newSurveyCreator);

}, []);
于 2020-12-17T12:45:10.263 回答
0

为了更好地实施,我使用来自的动态导入解决了这个问题NextJs

相反,我SurveyComponent像这样导入了我的:

const SurveyComponent = dynamic(
  () => import("../../../../components/survey/PartOne"),
  {
    ssr: false,
  }
);

确保我SurveyComponent不会在服务器端呈现。

于 2022-01-02T17:44:11.943 回答