I am new to Next.JS and having a hard time adjusting myself to it. Especially around initializing an imported module in my useEffect()
, as the module uses window
which ssr cannot handle. Since it is not a component, I am not using the dynamic() here.
What I want to do is execute functions in the imported module, however it is giving me var.func is not a function
error. I have tried several combinations, but none of them worked.
My attempt 1 - Use the variable which imported the module.
useEffect(() => {
const runInit = async () => {
const a = await import('MODULE_PATH')
try {
await a.init();
} catch (e) {
}
}
runInit()
},[]);
My attempt 2 - Store the imported module in a state.
const [mod, setMod] = useState(null);
useEffect(() => {
const runInit = async () => {
const a = await import('MODULE_PATH')
setMod(a)
try {
await mod.init();
} catch (e) {
}
}
runInit()
},[]);
I also tried to set let m = new a
but it did not work either. Can anyone please help me with calling functions in the imported module?