Skip to content

Commit 24a21c8

Browse files
committed
refactor: Better editor mount/unmount setup
1 parent e945649 commit 24a21c8

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/components/code-editor/index.jsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ const highlightStyle = HighlightStyle.define([
2727
]);
2828

2929
export default function CodeEditor(props) {
30-
const ref = useRef(null);
30+
const editorParent = useRef(null);
31+
const editor = useRef(null);
3132
// eslint-disable-next-line no-unused-vars
3233
const [_, setEditor] = useState(null);
3334

3435
useEffect(() => {
36+
if (editor.current && !props.baseExampleSlug) return;
37+
if (editor.current) editor.current.destroy();
38+
3539
const theme = EditorView.theme({}, { dark: true });
3640

3741
const state = EditorState.create({
@@ -56,18 +60,20 @@ export default function CodeEditor(props) {
5660
]
5761
});
5862

59-
const view = new EditorView({
63+
editor.current = new EditorView({
6064
state,
61-
parent: ref.current
65+
parent: editorParent.current
6266
});
6367

64-
setEditor(view);
68+
setEditor(editor.current);
69+
}, [props.baseExampleSlug]);
6570

66-
return () => {
67-
view.destroy();
71+
useEffect(() => (
72+
() => {
73+
editor.current.destroy();
6874
setEditor(null);
69-
};
70-
}, [props.baseExampleSlug]);
75+
}
76+
), []);
7177

72-
return <div ref={ref} class={cx(style.codeEditor, props.class)} />;
78+
return <div ref={editorParent} class={cx(style.codeEditor, props.class)} />;
7379
}

src/components/controllers/repl/index.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ export class Repl extends Component {
193193
if (this.state.exampleSlug) {
194194
const example = getExample(this.state.exampleSlug, EXAMPLES);
195195
if (code !== example.code && this.state.exampleSlug !== '') {
196+
// eslint-disable-next-line react/no-did-update-set-state
197+
this.setState({ exampleSlug: '' });
196198
history.replaceState(null, null, '/repl');
197199
}
198200
}

0 commit comments

Comments
 (0)