Skip to content

Commit

Permalink
Merge pull request #2292 from quantified-uncertainty/fix-dev-editor
Browse files Browse the repository at this point in the history
Fix code editor in strict mode
  • Loading branch information
berekuk authored Sep 25, 2023
2 parents 323e358 + c4cd89b commit 5a3c70d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions packages/components/src/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
},
ref
) {
// `useState` instead of `useMemo`, because we want to initialize view only once.
// We'll handle further updates through `useEffect` calls.
const [view] = useState(() => {
const [view, setView] = useState<EditorView>();

useEffect(() => {
if (typeof window === "undefined") {
return undefined; // CodeMirror view is not SSR-compatible
return; // no SSR
}

const extensions = [
highlightSpecialChars(),
history(),
Expand Down Expand Up @@ -142,15 +141,13 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
doc: defaultValue,
extensions,
});
const view = new EditorView({ state });
return view;
});

useEffect(() => {
setView(new EditorView({ state }));
return () => {
view?.destroy();
};
}, [view]);
// we initialize the view only once; no need for deps
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const format = useCallback(async () => {
if (!view) return;
Expand Down

3 comments on commit 5a3c70d

@vercel
Copy link

@vercel vercel bot commented on 5a3c70d Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 5a3c70d Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 5a3c70d Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.