From daf3118b70ecfd022ae78f00638c0ca34998896d Mon Sep 17 00:00:00 2001 From: MrWindlike Date: Mon, 14 Feb 2022 15:19:47 +0800 Subject: [PATCH] fix(editor): fix crash after errors hanpened --- packages/editor/src/components/Editor.tsx | 37 +++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/editor/src/components/Editor.tsx b/packages/editor/src/components/Editor.tsx index 978aa90a..9d41ca0d 100644 --- a/packages/editor/src/components/Editor.tsx +++ b/packages/editor/src/components/Editor.tsx @@ -67,13 +67,6 @@ export const Editor: React.FC = observer( const [recoverKey, setRecoverKey] = useState(0); const [isError, setIsError] = useState(false); const [store, setStore] = useState(stateStore); - useEffect(() => { - const stop = watch(stateStore, newValue => { - setStore({ ...newValue }); - }); - - return stop; - }, [stateStore]); const onError = (err: Error | null) => { setIsError(err !== null); @@ -133,6 +126,23 @@ export const Editor: React.FC = observer( ); }, [App, app, gridCallbacks, recoverKey]); + useEffect(() => { + // when errors happened, `ErrorBoundary` wouldn't update until rerender + // so after the errors are fixed, would trigger this effect before `setError(false)` + // the process to handle the error is: + // app change -> error happen -> setError(true) -> setRecoverKey(recoverKey + 1) -> app change -> setRecoverKey(recoverKey + 1) -> setError(false) + if (isError) { + setRecoverKey(recoverKey + 1); + } + }, [app, isError]); // it only should depend on the app schema to update + useEffect(() => { + const stop = watch(stateStore, newValue => { + setStore({ ...newValue }); + }); + + return stop; + }, [stateStore]); + const renderMain = () => { const appBox = ( @@ -151,9 +161,7 @@ export const Editor: React.FC = observer( position="absolute" /> - - {appComponent} - + {appComponent} @@ -220,6 +228,9 @@ export const Editor: React.FC = observer( services={services} /> + + + @@ -279,12 +290,6 @@ export const Editor: React.FC = observer( ); }; - useEffect(() => { - if (isError) { - setRecoverKey(recoverKey + 1); - } - }, [app, isError, recoverKey]); - return (