Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Last active March 6, 2022 21:36
Show Gist options
  • Save Lightnet/a1c451ab87306b338d2448d849cb37b9 to your computer and use it in GitHub Desktop.
Save Lightnet/a1c451ab87306b338d2448d849cb37b9 to your computer and use it in GitHub Desktop.
Reactjs AceEditor.jsx simple ssr check

This is simple check for server ssr in case of window null. When doing the server and client prerender or ssr. Reactjs, vitejs, expressjs tested.

For react-ace editor or other editor.

import React, { useEffect, useState } from "react";
export default function AceEditor(props){
const [Ace, SetAce] = useState(null);
useEffect( async()=>{
if (typeof window !== 'undefined') {//server ssr
const ReactAce = await import("react-ace"); //load in order
//await import("ace-builds/src-noconflict/mode-java"); //load in order
//await import("ace-builds/src-noconflict/mode-javascript"); //load in order
await import("ace-builds/src-noconflict/mode-jsx"); //load in order
await import("ace-builds/src-noconflict/theme-github"); //load in order
SetAce(ReactAce)// when all module is load then set ace editor.
}
},[])
if(Ace){
//console.log(Ace)
const AceComp = Ace.default;
return <AceComp {...props}/>
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment