From 5670f8b1918f9bf588735780c4b1469d0356b2cf Mon Sep 17 00:00:00 2001 From: Mario Buikhuizen Date: Thu, 14 Dec 2023 14:37:48 +0100 Subject: [PATCH] fix: useEffect cleanup not executed when re-executing cell --- src/widget.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/widget.tsx b/src/widget.tsx index 44a58f7..97add1c 100644 --- a/src/widget.tsx +++ b/src/widget.tsx @@ -22,6 +22,7 @@ import { MODULE_NAME, MODULE_VERSION } from './version'; // import 'es-module-shims'; import { transform } from 'sucrase'; import { ErrorBoundary } from './components'; +import { Root } from "react-dom/client"; // @ts-ignore @@ -108,6 +109,8 @@ export class ReactModel extends DOMWidgetModel { export class ReactView extends DOMWidgetView { + private root: Root | null = null; + render() { // @ts-ignore window.React18FromIpyReact = React; @@ -280,12 +283,16 @@ export class ReactView extends DOMWidgetView { } } if(this.model.get("react_version") === 18) { - const root = ReactDOMClient.createRoot(this.el); - root.render(); + this.root = ReactDOMClient.createRoot(this.el); + this.root.render(); } else { // @ts-ignore // ReactDOM16.render(, this.el); } } + + remove() { + this.root?.unmount(); + } }