Skip to content

Commit

Permalink
fix: site editor hmr bug
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong committed Jun 17, 2024
1 parent c82817e commit fc097f4
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions site/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Parser } from '@rekajs/parser';
import { RekaProvider } from '@rekajs/react';
import { useRouter } from 'next/router';
import * as React from 'react';
import { flushSync } from 'react-dom';

import { HeaderToolbar } from '@app/components/header/HeaderToolbar';
import { SEO } from '@app/components/seo';
Expand All @@ -25,6 +26,17 @@ const AppEditor = () => {
const routerRef = React.useRef(router);
routerRef.current = router;

const disposeEditor = React.useCallback(
(editor: Editor) => {
flushSync(() => {
setEditor(null);
});

editor.dispose();
},
[setEditor]
);

React.useEffect(() => {
if (!router.isReady) {
return;
Expand All @@ -37,10 +49,9 @@ const AppEditor = () => {
setEditor(editor);

return () => {
setEditor(null);
editor.dispose();
disposeEditor(editor);
};
}, [setEditor, router.isReady]);
}, [setEditor, disposeEditor, router.isReady]);

React.useEffect(() => {
if (!editor) {
Expand Down

0 comments on commit fc097f4

Please sign in to comment.