Skip to content

Instantly share code, notes, and snippets.

@MrMeison
Last active December 29, 2021 10:30
Show Gist options
  • Save MrMeison/1633d2e2825c6a8065a53a7145af20ad to your computer and use it in GitHub Desktop.
Save MrMeison/1633d2e2825c6a8065a53a7145af20ad to your computer and use it in GitHub Desktop.
const { useEffect, useRef } = require("react");
const noop = () => {};
class Editor {
constructor(element) {
this.element = element;
}
destroy() {
// ...
}
}
const useEditor = (containerRef) => {
const editorRef = useRef(null);
useEffect(() => {
if (!containerRef.current) {
return noop;
}
const editor = new Editor(containerRef);
editorRef.current = editor;
return () => {
editorRef.current.destroy();
editorRef.current = null;
};
}, [containerRef]);
return editorRef;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment