众所周知NextJS
,正在使用服务器端渲染,我想使用SurveyJs
它,但surveyJS
正在使用一些必须在客户端执行的功能。在我的情况下,我想使用StylesManager.applyTheme
,但它会引发服务器错误ReferenceError: document is not defined
我可以使用任何可能的方式applyTheme
在客户端执行该功能吗?
您可以通过创建一个使用外部函数来应用此主题的 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);
}, []);
为了更好地实施,我使用来自的动态导入解决了这个问题NextJs
相反,我SurveyComponent
像这样导入了我的:
const SurveyComponent = dynamic(
() => import("../../../../components/survey/PartOne"),
{
ssr: false,
}
);
确保我SurveyComponent
不会在服务器端呈现。