Open Lite5h4dow opened 3 years ago
Same problem here. Any hint?
no nothing atm thinking i might switch to next-electron-server but that has issues too. this is a huge part of the plan i have for this app
I'm not sure if I'm reading this wrong but wouldn't you just need to ensure that the component is not rendered unless the window is loaded? From my understanding dynamic is more a way to treeshake and codesplit.
If that is the case you can just use a component load use effect to set a state when dom is "ready".
import React from "react";
import Head from "next/head";
import OverlayStage from "../components/OverlayStage";
const Next = () => {
const [windowReady, setWindowReady] = useState(false);
useEffect(() => {
setWindowReady(true)
}, []) // (similar to onComponentMount)
return (
<React.Fragment>
<Head>
<title>Nuzlocke Legends - Overlay</title>
</Head>
{ windowReady && <OverlayStage /> }
</React.Fragment>
);
};
export default Next;
or you might not need the useEffect and useState at all by wrapping the component in a conditional checking if window is a thing
{ window && <OverlayStage /> }
Hope any of this helps, (untested code above just wrote what I thought would work.
No because that fails during build and test too.
Any answer for this? i tried using create-next-app with the exact same code and no problem occured..
Having the same issue here. Import working fine in NextJS but not in Nextron... Anyone help?
For more information on why this happens. It is because the build process exports the next project statically. You can see this in the build command here See Next Docs for information on that command
It may be happening because there will not be any SSR in the build export stage. You can try doing something like this which you can make a conditional to only load when window present. However this is likely to not work as the code might never load, but you can always give it a shot.
Thank you! For anyone else having this issue, I'll post a little code snipped below:
let importedComponent = null
if (window !== undefined) {
const importing = require("insert path here");
const myComponent = importing.default //can also be a different export
importedComponent = <myComponent/>
} else { //for build purposes only
importedComponent = <div><p>Component not available.</p></div>;
}
//when returning:
return(
<div>
{importedComponent}
</div>
)
That Solution works for me
Thank you! For anyone else having this issue, I'll post a little code snipped below:
let importedComponent = null if (window !== undefined) { const importing = require("insert path here"); const myComponent = importing.default //can also be a different export importedComponent = <myComponent/> } else { //for build purposes only importedComponent = <div><p>Component not available.</p></div>; } //when returning: return( <div> {importedComponent} </div> )
For some reason your answer not working on mine. so i change conditional statement to
global?.window && window !== undefined
and it's working.
@gabriel-visualthinking
Thank you! For anyone else having this issue, I'll post a little code snipped below:
let importedComponent = null if (window !== undefined) { const importing = require("insert path here"); const myComponent = importing.default //can also be a different export importedComponent = <myComponent/> } else { //for build purposes only importedComponent = <div><p>Component not available.</p></div>; } //when returning: return( <div> {importedComponent} </div> )
To keep this much less verbose it may be possible to write it as the following
return (
<div>
{window !== undefined ? (
require("path").default
) : (
<p>Component not available.</p>
)}
</div>
);
if that does not work
const importedComponent = window !== undefined ? require("path").default : <p>Component not available</p>
return (
<div>
<importedComponent />
</div>
);
Should also, reducing boilerplate.
Let me know if any of these work and if none I can remove this comment. ( I have just wrote syntax from memory )
Im trying to use PixiJS & React-Pixi and despite wrapping it in a component imported with a dynamic import it still cant compile in the dev or build environment.
console error
component that references react-pixi (i know the dynamic export is a little overkill, but i was trying everything)
OverlayStage.tsx
component that imports the isolated component
overlay.tsx